Does “Atomic Operations in MongoDB” have order of execution?

167 views
Skip to first unread message

MongoUser

unread,
Aug 30, 2012, 6:40:59 AM8/30/12
to mongod...@googlegroups.com
Which is order of execution of atomic operations in the following update method?

     db.foo.update({'_id': 'a'}, {'$push': {'things': 'thing1'}, '$inc': {'version': 1}

The First is '$push' and the second if '$inc' ?
Or the order of execution is unknown?

Scott Hernandez

unread,
Aug 30, 2012, 6:50:47 AM8/30/12
to mongod...@googlegroups.com

The order is undefined. But both will happen to the document atomically; no read can happen in the middle of the write.

--
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
See also the IRC channel -- freenode.net#mongodb

MongoUser

unread,
Aug 30, 2012, 6:58:32 AM8/30/12
to mongod...@googlegroups.com
Please look at the following code:

     db.foo.update({'_id': 'a'}, {'$pull' : {'things': 'thing1'} '$push': {'things': 'thing2'},)

I need to first pull 'thing1' and then push 'thing2' Is this possible in Mongodb?

четверг, 30 августа 2012 г., 14:50:56 UTC+4 пользователь Scott Hernandez написал:

Scott Hernandez

unread,
Aug 30, 2012, 7:32:49 AM8/30/12
to mongod...@googlegroups.com
You will want to watch and vote for this feature for that
functionality: https://jira.mongodb.org/browse/SERVER-1050

MongoUser

unread,
Aug 30, 2012, 7:45:59 AM8/30/12
to mongod...@googlegroups.com
But how it helps me if you have answered that the order of execution is undefined? What will be first $pull or $push?

четверг, 30 августа 2012 г., 15:33:19 UTC+4 пользователь Scott Hernandez написал:

Scott Hernandez

unread,
Aug 30, 2012, 9:03:59 AM8/30/12
to mongod...@googlegroups.com
It is not supported and the issue I posted is the feature to enable that.

Have you tried to do it?

This is what you will get:
> db.a.save({_id:2, arr:[1,2,3]})
> db.a.update({_id:2}, {$push:{arr:1}, $pull:{arr:1}})
Field name duplication not allowed with modifiers

MongoUser

unread,
Aug 30, 2012, 9:29:20 AM8/30/12
to mongod...@googlegroups.com
Thanks you Scott. I do understand that it's not supported yet. I've asked about if this will be realized in a future does it help me? Because as you  wrote The order is undefined. Because in my case I to $pull first and only then $push:

      db.foo.update({'_id': 'a'}, {'$pull' : {'things': 'thing1'} '$push': {'things': 'thing2'});

So I need a specific order when I use a 'Atomic Operations'. 

четверг, 30 августа 2012 г., 17:04:29 UTC+4 пользователь Scott Hernandez написал:

Scott Hernandez

unread,
Aug 30, 2012, 1:07:58 PM8/30/12
to mongod...@googlegroups.com
Yeah, in order for it to work we would need to define an order and
that is a tricky problem depending on the operators and use-cases.
Specifying an explicit order could be complicated as well.

Feel free to add your thoughts to the issue/feature-request.
Reply all
Reply to author
Forward
0 new messages