I have a client document that contains contacts as an array of sub-documents.
When I send the client document to the DB it should behave in the following manner:
If the document doesn't exist, create the document along with all sub documents.
If the document does exist, then add missing contacts (multiple) to array of sub documents.
After doing some research I found something that matches what I'd like to do but it seems to work only with a single additional sub document instead of an array of sub documents.
I am also having a really hard time recreating this using the mongodb c# driver.
Does anyone know if its possible to do this in one query?
This is what I have found so far:
db.clients.update({$and:[{'clientName':'apple'},{'contacts.firstName': {$ne: 'nick'}},{'contacts.lastName': {$ne: 'white'}}]},
{$set:{'clientName':'apple'}, $push: {contacts: {'firstName': 'nick', 'lastName':'white'}}},
{upsert: true });