Search code examples
databasemongodbsocial-media-like

MongoDB multiple simultaneous accesses to one document


I am building a social network app and I am very concerned about next thing.What happens when in mongoDB a lot of users(let's assume millions) try to modify the same document at the same time. Will there be any mismatch ,or ignored queries or any kind of unexpected behaviour?

Practical Example: 2 collections: 'posts' and 'likes' posts will have fields id | name | info | numberOfLikes likes will have fields id | post | fromUser

When assumed millions of users like the post ,like object appears in 'likes' collection and business logic automatically increments numberOfLikes for post. I thought if there could be a conflict when tons of users try to modify that post likes count at the same time.


Solution

  • Databases have mechanisms in place to prevent this kind of situation. You can 'lock' on various logical structures, so you can be assured your data is intact - regardless of your transaction count.

    See more below:

    http://docs.mongodb.org/manual/faq/concurrency/