skip to Main Content

I want to replace all documents in my collection and replace them with new documents.

So far, I tried using deleteMany() then create() but there seems to be a small latency between the 2 operations. So is there any way I can delete everything and create documents at the same time?

2

Answers


  1. You might try replaceMany; since that’s an atomic operation, it’s considered as a single operation without any latency between deleting the old documents and creating the new ones.

    Login or Signup to reply.
  2. If you want to keep the old object IDs, the replace or update method will be the solution. If not – delete and then сreate. Also you can combine it all in BulkWrite method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search