I have a MongoDB document something like this:
    "_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"
        }
    ]
In the field sharingInfo i want to insert a document in a behaviour mentioned below:
If the _id fields with same value already exists then update the value of accessType otherwise create a fresh new entry.
I will try to explain with two examples.
Example 1
Let's suppose i am trying to insert a document like this:
  {
    "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
    "accessType" : "edit"
  }
As this _id already exists, the resultant sharingInfo should like:
    "sharingInfo" : [ 
        {
            "_id" : ObjectId("5c715d627adcfb7980c5d6c2"),
            "accessType" : "edit"
        }, 
        {
            "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
            "accessType" : "edit"
        }
    ]
Example 2
Let's suppose i am trying to insert a document like this:
  {
    "_id" : ObjectId("5ccb4070d0c24b35d4cbb520"),
    "accessType" : "view"
  }
As this _id doesn't already exists, the resultant sharingInfo should like:
    "sharingInfo" : [ 
        {
            "_id" : ObjectId("5c715d627adcfb7980c5d6c2"),
            "accessType" : "edit"
        }, 
        {
            "_id" : ObjectId("5c72acf12f63693410f2ae1a"),
            "accessType" : "edit"
        },
        {
            "_id" : ObjectId("5ccb4070d0c24b35d4cbb520"),
            "accessType" : "view"
        }
    ]
I came across the operator $addToSet but it compares the entire document, i can't make it compare based on a particular field _id
If i can drop duplicates based on certain conditions that will also work, but i found dropDups is no longer available with recent versions of MongoDB. 
When i want to update sharingInfo with some data, i receive the data in an array of Object format:
For example:
[
  {
    "_id" : ObjectId("7d72acf12f63693410f2ae1b"),
    "accessType" : "edit"
  },
  {
    "_id" : ObjectId("8acb4070d0c24b35d4cbb521"),
    "accessType" : "view"
  }
]
If i try to perform a $set operation the entire content of sharingInfo gets replaced with the new data.
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.
I am using mongoose to model my data.
Is there any efficient way to achieve the entire operation ?
I can perform multiple read write operation in the DB but that will be highly inefficient, don't want that.
This is something update if exists otherwise insert in an array of documents
PS: I am new to MongoDB.
Thanks in advance.
 
     
    