0

For example< we have a next db structure:

  • Users
  • Items
  • Games

Items belongs to Users (userId field in item) aka inventory. Users can create games. Users can join created games. Only two users allowed to join a game. When game is completed, all items from users inventories should be transferred to "winner" inventory.

I want to build a flow, by using NodeJs, to handle "joinGame", "winGame" cases. In a MySql database i used transactions to lock "game" record and perform all operations in a single transaction to ensure data consistency.

I have no idea how to make it in mongo in a correct way. Digging on internet, i found two solution:

  1. Write a worker, what cycles through a "game" records to finalize them.
  2. Use app-level (node executable) lock
  3. ?

Thank you in advance!

  • 1
    In general the concept is "don't join" and put all the "related" data into a single document. This is how you live without transactions. It's not a relational database, so if you are just going to try doing things the same way, then there's little point in using a different storage engine in the first place. – Neil Lunn Nov 02 '17 at 05:39
  • 1
    https://stackoverflow.com/a/39210371/2313887 – Neil Lunn Nov 02 '17 at 05:41
  • Thank you very much. I have another q: for example i want to transfer inventory items from one document to another using query like sql “where findinset(item.id, ids)”. Is there a way in mongo to do it in a single atomic operation? Or it would be two: remove, add? – Olexander Korenyuk Nov 02 '17 at 06:55

0 Answers0