javascript - I think this code involving NodeJs and MongoDb might fail, how to fix it? -


It looks like a normal database query, but I will ask it in the current context of my node + MongoDb.

We say that ID is some universal counter with "xyz". Get the latest calculation and 1:

  collection.find ({id: "xyz"}, function (mistake, presentcount) {collection.update ({id: "xyz"}, {$ Inc: {count: 1}}, function (error, data) {cb (currentCount);}}}   

The problem is, if two clients call this code at the same time Trying to do it, then both clients can get a single count, then collects the collection twice - not desirable! How to fix this?

$ inc There is no danger in it, it is from external search that does not work. collection.update ({id: "xyz"}, {$ inc: {count: 1 }}, Function (fault, data) {cb (data);}}

Chances are what you want.

Comments