updating the first result using sort / order

20 views
Skip to first unread message

Tom

unread,
Nov 5, 2012, 12:18:10 PM11/5/12
to mongod...@googlegroups.com
I would like to update the first field returned by a query using a sort condition. Is this possible? At the moment I need to use findAndModify to do this, as it provides a `sort` option, like so:

videos.findAndModify({pos: 0, [['index', 1]], {$set: {pos: 1}}, function(err, doc) {});

The above updates the first video with a `pos` of 0 to a `pos` of 1. However, this returns the old document whereas I do need it.

Therefore it would be more appropriate and less resource consuming for me to use the update command. However, it does not seem to have a sort parameter. I would like to use it like so:

videos.update({pos: 0, [['index', 1]], {$set: {pos: 1}}, {safe: false});

Achieving the same objective while not returning the original object.

Is this possible?

(Note that my examples use the Node.js driver, which uses Javascript and should therefore be almost identical to Mongo's console Javascript syntax).

Tom

Tom

unread,
Nov 6, 2012, 11:04:10 AM11/6/12
to mongod...@googlegroups.com
If my question is unclear please do not hesitate to ask for more information.

Thank you.

Op dinsdag 6 november 2012 00:18:32 UTC+7 schreef Tom het volgende:

Octavian Covalschi

unread,
Nov 6, 2012, 11:08:11 AM11/6/12
to mongod...@googlegroups.com
You can tell findAndModify what fields to return, have you done that?


--
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

Tom

unread,
Nov 6, 2012, 11:23:06 AM11/6/12
to mongod...@googlegroups.com
That sounds like a good workaround, by telling it to not return anything.

Unfortunately my Node.js driver does not seem to have such option though (source: http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#findandmodify).

Is this a problem with the driver?

Octavian Covalschi

unread,
Nov 6, 2012, 11:25:27 AM11/6/12
to mongod...@googlegroups.com
maybe, but I wonder if that would go into "[options] (object) – additional options during update."
Reply all
Reply to author
Forward
0 new messages