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
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.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.