How do I update MongoDB document fields only if they don't exist?

后端 未结 6 829
春和景丽
春和景丽 2020-12-08 09:03

I have collection foo with documents like:

{site_id: \'xxx\', title: {ru: \'a\', en: \'b\'}, content:         


        
6条回答
  •  攒了一身酷
    2020-12-08 10:10

    @nutlike's answer does solve the issue, however, if you would like to update multiple fields on the item it will require many database operations. In short, what you want is not exactly possible.

    If your doc has more updates to do than you'd like to do one at a time (any greater than 2, IMO), then you should just get the document, update the fields and then save it. This is what I do on a couple of OAuth user creating/updating routes.

提交回复
热议问题