I have a collection which looks like this (fields removed for brevity):
[
{ "id": "A", "name": "AA" ,"arrayField": [1, 2, 3] },
{ "id": "B", "name": "BB" ,"arrayField": [4, 5, 6] },
{ "id": "C", "name": "CC" ,"arrayField": [7, 8] }
]
Consider each object as a document.
Now in my application, after some business logic, I have an array of POJOs which look like this:
[
{ "id": "A", "name": "AB", "arrayField": [1, 2, 3] },
{ "id": "B", "name": "VB", "arrayField": [4, 5, 6] },
{ "id": "C", "name": "DE", "arrayField": [7, 8] }
{ "id": "D", "name": "GG", "arrayField": [10, 11] }
]
Note that these objects don’t have the other fields required by the document.
How can I update the MongoDB collection to reflect the same as in the array of objects?
- I would like to update only the
arrayFieldand thenamefields of documents withidA, B** and C to reflect the array of objects. - There is also a new document with
idD which have to be inserted in the collection (the other fields will be taken care of as they have default values in the schema).
Things I’ve tried/thought of
Note: arr is the array of objects being talked about.
-
updateManywithupsert:Model.updateMany({ id: { $in: arr.map(({ id }) => id) } }, arr, { upsert: true }); -
Mapping over the array of objects and updating with upsert:
await Promise.all( arr.map(({ id, arrayField, name }) => { return Model.updateOne({ id }, { $set: { arrayField, name } }, { upsert: true }); }) );
Is there any way to do this using transactions/aggregations/etc.?