MongoDB: upsert with different values for update and insert

旧时模样 提交于 2019-12-24 19:08:59

问题


A little context: I have a document for each user that contains an array with latest 20 events related to a user. As MongoDB does not have this feature(to cap arrays inside a document), I will push my event and pop the latest one.

My problem: initializing the document(aka filling array with nulls). I want to atomically:

  • create document containing an array with 20 null values and push one value, if document does not exist

or

  • update document (push one value in array), if document exists

Do you have any other suggestions? A hack I thought about would be to declare a index with :unique and :dropDups, and to always make an initialization insert.

Related to: MongoDB fixed size array implementation


回答1:


Not possible in a single operation, yet. You want http://jira.mongodb.org/browse/SERVER-991 or http://jira.mongodb.org/browse/SERVER-453.



来源:https://stackoverflow.com/questions/4525716/mongodb-upsert-with-different-values-for-update-and-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!