I'm using mongo and I have multiple queries to insert at a time so I use a for loop to insert into the db. The problem is that each query falls under a key so I check if a key exists of not and if it doesn't I add it to the db, if it does, I append it. If I have multiple queries with the same key (since mongo inserts asynchronously) these two same keys could be identified as "nonexistent" in the db since they could be running in parallel. Is there a way around this?
Asked
Active
Viewed 626 times
0
-
Chances are that your case is not actually as complex as you are making it sound. Show some some example updates and overall result you are trying to achieve in your question. There are techniques in both bulk operations use and basic upserts that will likely suit your purpose. But we need some more concrete examples to be sure. – Neil Lunn May 21 '17 at 22:27
1 Answers
0
If you're writing a lot of documents you're probably better off using bulk operations in mongo https://docs.mongodb.com/manual/core/bulk-write-operations/.
You can write the queries as upserts. this questions is very similar I think to what you are trying to accomplish. How to properly do a Bulk upsert/update in MongoDB.
If you do it as an ordered bulk operation you should not have the problem with two queries running simultaneously.

Community
- 1
- 1

ThrowsException
- 2,586
- 20
- 37