title: mongoDB upsert on array
body: <p>my db model looks like this        </p>
  
  <pre><code>"clientId":"123456"
  "devices" : 
  [{
        "deviceId" : "123",
        "deviceType" : "ios",
        "notification" : true,
    }
    {
        "deviceId" : "321",
        "deviceType" : "android",
        "notification" : true,
  
    }
   ]
  </code></pre>
  
  <p>The c# model I pass to my DB method has ClientId,DeviceId,DeviceType,notification. Note that it doesn't have an array of devices</p>
  
  <p>the behaviour I'm trying to achieve</p>
  
  <ol>
  <li><p>(working) If ClientId is not yet in the database, create a new record in db with the passed value,(devices array will have one element) </p></li>
  <li><p>If ClientId is already found in the database, update the the record according to the following rules:</p>
  
  <p>2.1 If devices array doesn't contain DeviceId add a new element to the array with the passed values</p>
  
  <p>2.2 If devices array already contains DeviceId update the element's deviceType and notification.</p></li>
  </ol>
  
  <p>I'm using c#. any help appreciated</p>
  
  <p>example: given the above state of database, by passing clientId: 123456, deviceId 321 deviceType "kindle", notification "false" the db will change to </p>
  
  <pre><code>"clientId":"123456"
  "devices" : 
  [{
        "deviceId" : "123",
        "deviceType" : "ios",
        "notification" : true,
    }
    {
        "deviceId" : "321",
        "deviceType" : "kindle",
        "notification" : false,
  
    }
   ]
  </code></pre>