Insert or Update

I have a problem where i get multiple Items to be Created or Updated lets say

{
   Id: 123,
   something: hey,
   TotalSupply: 1,
}

But i’m getting items fast so very frequently the i get another item with the same Id which should make the totalSupply +1, But this item wasn’t yet created so the Find() is not finding it yet so its creating another item and i’m getting duplicates instead of incrementing the supply,

If i use the ObjectId From mongo i get errors trying to create item with similar Id

1 Like

Your problem is that you rely on find() to determine what to do.

This is wrong for any database system, since like you have seen your data might be created, edited or deleted by some other process.

Look at https://docs.mongodb.com/manual/reference/method/db.collection.update/.

I strongly recommend you take some courses from https://university.mongodb.com/.

1 Like