Vlad Zloteanu
Vlad Zloteanu

Reputation: 8512

MongoDB: upsert with different values for update and insert

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:

or

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

Upvotes: 2

Views: 2100

Answers (2)

scotts
scotts

Reputation: 4097

You'll want to try: $setOnInsert

Upvotes: 0

kris
kris

Reputation: 23592

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

Upvotes: 1

Related Questions