Is moving documents between collections a good way to represent state changes in MongoDB?

后端 未结 2 740
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 06:06

I have two collections, one (A) containing items to be processed (relatively small) and one (B) with those already processed (fairly large, wit

2条回答
  •  有刺的猬
    2021-02-13 06:41

    There's no way to atomically remove+save to different collections, as far as I can tell (maybe by design?)

    Yes this is by design. MongoDB explicitly does not provides joins or transactions. Remove + Save is a form of transaction.

    Is there a Best Practice for this situation?

    You really have two low-complexity options here, both involve findAndModify.

    Option #1: a single collection

    Based on your description, you are basically building a queue with some extra features. If you leverage a single collection then you use findAndModify to update the status of each item as it is processing.

    Unfortunately, that means you will lose this: ...that the "incoming" collection can be kept very small and fast this way.

    Option #2: two collections

    The other option is basically a two phase commit, leveraging findAndModify.

    Take a look at the docs for this here.

    Once an item is processed in A you set a field to flag it for deletion. You then copy that item over to B. Once copied to B you can then remove the item from A.

提交回复
热议问题