Add or update exist object using insertMany

I’ve an array of objects and i need to add these objects to DB, in case the object doesn’t exist in collection otherwise update it if it’s already exist

Hi,

You can do it with upsert config in update query.

I’ve array of objects and i need to update the document if it’s exist in the array or push it if it’s not.

ex:

I have this array: [

{key: 1, name: ‘s’},

{key: 2, name: ‘n’},

{key: 3, name: ‘r’}

]


db collection documents:

[

{key: 1, name: ‘s’}, {key: 2, name: ‘t’}

]


i need the result to be like this in collection:

[

{key: 1, name: ‘s’}, {key: 2, name: ‘n’}, {key: 3, name: ‘r’}

]