title: MongoDB add items to array of documents if not exists already based on a particular field
body: <p>I have a MongoDB document something like this:</p>
  
  <pre><code>    "_id" : ObjectId("5ccb4067d0c24b35d4cbb51e"),
      "title" : "Sharing test 1",
      "content": "Test Content",
      "creator" : ObjectId("5c715d627adcfb7980c5d4c2"),
      "createdAt" : ISODate("2019-05-03T00:39:27.594+05:30"),
      "lastUpdatedAt" : ISODate("2019-05-03T00:39:27.609+05:30"),
      "sharingInfo" : [ 
          {
              "_id" : ObjectId("5c715d627adcfb7980c5d6c2"),
              "accessType" : "edit"
          }, 
          {
              "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
              "accessType" : "view"
          }
      ]
  </code></pre>
  
  <p>In the field <code>sharingInfo</code> i want to insert a document in a behaviour mentioned below:</p>
  
  <p>If the <code>_id</code> fields with same value already exists then update the value of <code>accessType</code> otherwise create a fresh new entry.</p>
  
  <p>I will try to explain with two examples.</p>
  
  <p>Example 1</p>
  
  <p>Let's suppose i am trying to insert a document like this:</p>
  
  <pre><code>  {
      "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
      "accessType" : "edit"
    }
  </code></pre>
  
  <p>As this <code>_id</code> already exists, the resultant <code>sharingInfo</code> should like:</p>
  
  <pre><code>    "sharingInfo" : [ 
          {
              "_id" : ObjectId("5c715d627adcfb7980c5d6c2"),
              "accessType" : "edit"
          }, 
          {
              "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
              "accessType" : "edit"
          }
      ]
  </code></pre>
  
  <p>Example 2</p>
  
  <p>Let's suppose i am trying to insert a document like this:</p>
  
  <pre><code>  {
      "_id" : ObjectId("5ccb4070d0c24b35d4cbb520"),
      "accessType" : "view"
    }
  </code></pre>
  
  <p>As this <code>_id</code> doesn't already exists, the resultant <code>sharingInfo</code> should like:</p>
  
  <pre><code>    "sharingInfo" : [ 
          {
              "_id" : ObjectId("5c715d627adcfb7980c5d6c2"),
              "accessType" : "edit"
          }, 
          {
              "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
              "accessType" : "edit"
          },
          {
              "_id" : ObjectId("5ccb4070d0c24b35d4cbb520"),
              "accessType" : "view"
          }
      ]
  </code></pre>
  
  <p>I came across the operator <code>$addToSet</code> but it compares the entire document, i can't make it compare based on a particular field <code>_id</code></p>
  
  <p>If i can drop duplicates based on certain conditions that will also work, but i found <code>dropDups</code> is no longer available with recent versions of MongoDB. </p>
  
  <p>When i want to update <code>sharingInfo</code> with some data, i receive the data in an array of Object format:
  For example:</p>
  
  <pre><code>[
    {
      "_id" : ObjectId("7d72acf12f63693410f2ae1b"),
      "accessType" : "edit"
    },
    {
      "_id" : ObjectId("8acb4070d0c24b35d4cbb521"),
      "accessType" : "view"
    }
  ]
  </code></pre>
  
  <p>If i try to perform a <code>$set</code> operation the entire content of <code>sharingInfo</code> gets replaced with the new data.</p>
  
  <p>The idea here is to minimise number of API request, if someone wants to update the sharing info with multiple data, he/she can send a single API request with an Array of objects to be updated as payload.</p>
  
  <p>I am using mongoose to model my data.</p>
  
  <p>Is there any efficient way to achieve the entire operation ?</p>
  
  <p>I can perform multiple read write operation in the DB but that will be highly inefficient, don't want that.</p>
  
  <p>This is something update if exists otherwise insert in an array of documents</p>
  
  <p>PS: I am new to MongoDB.</p>
  
  <p>Thanks in advance.</p>