In MongoDB, you can use db.collection.save({_id:'abc'}, objectToSave) to do upsert.
Let define objectToSave as below
{_id:'abc', field1:1, field2:2};
In my collection, I already have a document with the same _id value, as shown below:
{_id:'abc', field3:3};
The save function above will replace the existing document in the collection with
{_id:'abc', field1:1, field2:2};
I want to perform the $ set operation to create some document in the collection below
{_id:'abc', field1:1, field2:2, field3:3};
Can this be achieved in the save function, or do I need to write separate update statements?
Note that the objectToSave fields are dynamic. The language I use is Node.JS.
source share