updates question

79 views
Skip to first unread message

David Judd

unread,
Aug 17, 2010, 4:43:46 PM8/17/10
to mongodb-user
I'm using MongoDB for a queue, and I'm doing a slightly complicated
upsert operation when adding objects, which are grouped while on the
queue. The command looks something like this:

{
"findandmodify" : "my_collection",
"query" : {
... <a multi-field _id> ...
},
"update" : {
"$addToSet" : {
"ids" : {
"$each",
[1, 2, 3, ...]
}
},
"$set" : { "timestamp" : 123 },
"$set" : { "priority" : 0 }
},
"upsert" true
}

(I'm actually using the Java driver, so I apologize for any JSON
errors.)

So far, so good. My problem is that for timestamp and priority, "$set"
isn't really what I want. I only want to set timestamp if there isn't
a timestamp on the existing row - I really want something like SQL
"coalesce(existing.timestamp, 123)". And for priority, I want the
maximum of the existing row and the added one - something like SQL
"greatest(existing.priority, 0)".

I know I can get basically the same result with a second query. And
I'll do that if I have to. But I wanted to ask if there's a way to do
it with just one. Is there some way to access the current value of the
row during the update operation?

Thanks!
-David

王海

unread,
Aug 17, 2010, 7:11:41 PM8/17/10
to mongod...@googlegroups.com
I just did a test below , maybe helpful to you.

Mongodb version 1.6

e.g.
> db.test1.find()
{ "_id" : 11, "name" : "wh" }
{ "_id" : 12, "name" : "ww" }
{ "_id" : 13, "name" : "wq" }
{ "_id" : 22 }
{ "_id" : 23 }
{ "_id" : 24 }
> db.test1.update({"name":{$exists:false}},{$set:{"name":"xxx"}},false,true)
> db.test1.find()
{ "_id" : 11, "name" : "wh" }
{ "_id" : 12, "name" : "ww" }
{ "_id" : 13, "name" : "wq" }
{ "_id" : 22, "name" : "xxx" }
{ "_id" : 23, "name" : "xxx" }
{ "_id" : 24, "name" : "xxx" }

-----邮件原件-----
发件人: mongod...@googlegroups.com [mailto:mongod...@googlegroups.com]
代表 David Judd
发送时间: 2010年8月18日 4:44
收件人: mongodb-user
主题: [mongodb-user] updates question

Thanks!
-David

--
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mongodb-user?hl=en.

David Judd

unread,
Aug 18, 2010, 10:58:04 AM8/18/10
to mongodb-user
Thanks, but that doesn't really do what I want it to. I want to update
some fields always, and other fields depending on their current value.
Unless I'm misreading, what you're doing will skip the rows which have
"name" field entirely.

-David

Michael Dirolf

unread,
Aug 18, 2010, 11:09:02 AM8/18/10
to mongod...@googlegroups.com
David,
There's no way to do the type of updates you're looking for currently,
in a single op. Feel free to file a feature request on JIRA if it's
something you'd like to see.
- Mike

2010/8/18 David Judd <david....@gmail.com>:

Reply all
Reply to author
Forward
0 new messages