0

I am trying to replace all documents with new values in bulk.

Example we have 500k docs in db and we have 500k the same docs with updated props inside. Now we need to update old data.

Idea was to use InsertMany with lean option to new collection and then remove the old one to get less number of reads/writes.

The question is there something easier for such a scenario?

Maybe even import/export is better in this case?

PS Model.updateMany() has a filter, we do not need filter here, because we know for sure that every document has updated properties, so we just need to replace them

Denys Medvediev
  • 1,160
  • 3
  • 16
  • 32

1 Answers1

1

There are different options.

1.insertMany to insert many documents. 2. UpdateMany with upsert will insert and update documents 3. Replace will replace the matching documents.

If your new doc has altogether new set of fields, you could use replace.

If only values changes and new fields, then you could use updateMany.

On the other hand, there is something called bulk Write. Explore that also.

Edit:

The best option for this use case is creating new collection everyday. Considering the number 500k is easy to handle. Swap the new and old collections.

Otherwise you could use replace option.

Gibbs
  • 21,904
  • 13
  • 74
  • 138