Petrus
unread,Sep 24, 2010, 9:09:25 AM9/24/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongodb-user
Hi,
Given the following document structure: {key:1, timestamp:
1285330185411, data:"the important stuff"}, where the timestamp
indicates the time when the data was valid.
I want to generate an atomic upsert which:
1. insert the document if the key does not exist
2. does nothing if a document with the key exists, but the
existing timestamp i newer.
3 updates the document if a document with the key exists and the
existing timestamp is older.
Here are some initial attempts that (obviously) does not work:
- This update does not apply to requirement 2:
db.col.update({key:1}, {key:1, timestamp:1285330185411,
data:"some new info"}, true)
- This update tries to take into account the "newer-feature", but
actually inserts a duplicate in case 2
db.col.update({key:1, timestamp:{"$gte":1285315416000}}, {key:1,
timestamp:1285330185411, data:"some new info"}, true)
Does anyone know if this is possible in MongoDb and how to do it?
Rgds, Petrus