Many to many update in MongoDB without transactions

前端 未结 3 928
死守一世寂寞
死守一世寂寞 2021-02-14 20:08

I have two collections with a many-to-many relationship. I want to store an array of linked ObjectIds in both documents so that I can take Document A and retrieve all linked Doc

相关标签:
3条回答
  • 2021-02-14 20:44

    Why don't you create a dedicated collection holding the relations between A and B as dedicated rows/documents as one would do it in a RDBMS. You can modify the relation table with one operation which is of course atomic.

    0 讨论(0)
  • 2021-02-14 20:46

    @Gareth, you have multiple legitimate ways to do this. So they key concern is how you plan to query for the data, (i.e.: what queries need to be fast)

    Here are a couple of methods.

    Method #1: the "links" collection

    You could build a collection that simply contains mappings between the collections.

    Pros:

    • Supports atomic updates so that data is not lost

    Cons:

    • Extra query when trying to move between collections

    Method #2: store copies of smaller mappings in larger collection

    For example: you have millions of Products, but only a hundred Categories. Then you would store the Categories as an array inside each Product.

    Pros:

    • Smallest footprint
    • Only need one update

    Cons:

    • Extra query if you go the "wrong way"

    Method #3: store copies of all mappings in both collections

    (what you're suggesting)

    Pros:

    • Single query access to move between either collection

    Cons:

    • Potentially large indexes
    • Needs transactions (?)

    Let's talk about "needs transactions". There are several ways to do transactions and it really depends on what type of safety you require.

    Should I use safe mode and manually check the data went in afterwards and try again on failure?

    You can definitely do this. You'll have to ask yourself, what's the worst that happens if only one of the saves fails?

    Method #4: queue the change

    I don't know if you've ever worked with queues, but if you have some leeway you can build a simple queue and have different jobs that update their respective collections.

    This is a much more advanced solution. I would tend to go with #2 or #3.

    0 讨论(0)
  • 2021-02-14 20:57

    Should I use safe mode and manually check the data went in afterwards and try again on failure?

    Yes this an approach, but there is an another - you can implement an optimistic transaction. It has some overhead and limitations but it guarantees data consistency. I wrote an example and some explanation on a GitHub page.

    0 讨论(0)
提交回复
热议问题