Enrico
Enrico

Reputation: 422

Concurrent access to a document with mongoose

I am writing a web application where more users can perform simultaneous operation in the same document in mongodb. I use the mean.io stack, but I am quite new to it. I was wondering how does mongoose manage concurrency. Every "user click" operation performs first a read to get the document, and a save after some calculations. Of course the sequence read-calculate-save is not atomic. Does mongoose work with 'last change wins' policy, or does it throw a versioning error? Does it make sense in this case to use a queue?

Thanks, best regards.

Upvotes: 6

Views: 4780

Answers (1)

saintmac
saintmac

Reputation: 589

Yes the last change will win.

A queue could be a good option to solve the problem but I'll suggest 2 other ways:

  1. You could use more advanced mongodb commands, such as $inc (http://docs.mongodb.org/manual/reference/operator/update/inc/) to compute attomically (if your computation are too complicated maybe it is not possible)
  2. If you don't necessarily need to have the correct count available at any time, you could use a 'big data' approach and just store the raw clicks information. Whenever you need the data (or say every hour or day), you could then use the mongodb aggregate framework, or their mapreduce feature, to compute the correct count.

Upvotes: 6

Related Questions