0
votes

I have mongoDB collection items with following document structure:

{ name: string, values: string[] }

Then I have large amount of documents outside of database, which I want add to the db.

  • If document with same name already exists in database, push its value to values of db item,
  • If document with same name doesn't exist, create new document.

For example, let's have these records in the database:

[
    { "name": "A", "values": ["Alaska"] },
    { "name": "B", "values": [] }
]

Now add these records:

[
    { "name": "A", "value": "Australia" },
    { "name": "C", "value": "Canada" }
]

Result should be:

[
    { "name": "A", "values": ["Alaska", "Australia"] },
    { "name": "B", "values": [] },
    { "name": "C", "values": ["Canada"] }
]

However the number of documents can be hundreds of thousands. Is there any better way to upsert array of records than one by one?

db.items.update({ "name": "A" }, { "$set": { "name": "A" }, "$push": { "values": "Australia" } }, { "upsert": true })
db.items.update({ "name": "C" }, { "$set": { "name": "C" }, "$push": { "values": "Canada" } }, { "upsert": true })
...
1

1 Answers

0
votes

You can use bulk writes to aggregate multiple updates into a single network request, but if the changes are individually determined per document and cannot be expressed as a query applying to multiple documents, each change must be its own insert/update command.