Abhimanyu Raizada
Abhimanyu Raizada

Reputation: 1

Mongodb prevent duplicate insertion of item due to parallel requests without indexing

I am trying to prevent duplicate insertion of an item into the collection due to multiple parallel requests at the same time.

My business logic is, if i dont have an unique item XYZ in the collection, I will insert it. Otherwise i will just return the document.

Now these item cannot be duplicate in db. In case of multiple concurrent requests in Nodejs, we are getting duplicate items in the database, as all the requests when read from database at same time, finds the item to not be present and then insert the item leading to duplication.

I know we can prevent this using unique indexes, but i don’t want to use indexes as the collection is very large and holds different kind of data, and we are already using heavy indexing on other collections.

Kindly suggest some other methods how can i handle this?

I can use indexes, but need other solution to avoid memory ram over usage.

Upvotes: 0

Views: 553

Answers (1)

LNorth
LNorth

Reputation: 86

Are you using insert? If so, I'd suggest using update with opts upsert=true. This, however, is only atomic when there is a unique index, according to this.

Other than that I don't know if you're using any sort of mutex for your concurrency, if not, you should look into it. Here is a good place to start.

Without either atomic operations or mutex locks, you're not guaranteed any data race safety in parallel threads.

Upvotes: 0

Related Questions