pymongo find_and_modify not honoring sort?

170 views
Skip to first unread message

Zac Witte

unread,
Dec 14, 2011, 7:21:44 PM12/14/11
to mongodb-user
I'm I missing something here?

ubuntu@mongo1:~/pubnub-analytics$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
>>> from pymongo import ASCENDING, DESCENDING
>>> conn = Connection()
>>> db = conn.pb2
>>> db.log_files.find_and_modify(
... query={'status':'unprocessed'},
... update={'$set':{}},
... sort=[('file_name',ASCENDING)],
... new=True)
{u'status': u'unprocessed', u'file_name':
u'2011-12-03_23:04:34.288091_50.18.36.76.log.bz2', u'_id':
ObjectId('4ee805931d011c644f003260')}
>>> db.log_files.find_one({'status':'unprocessed'})
{u'status': u'unprocessed', u'file_name':
u'2011-12-03_23:04:34.288091_50.18.36.76.log.bz2', u'_id':
ObjectId('4ee805931d011c644f003260')}
>>> db.log_files.find_one({'status':'unprocessed'}, sort=[('file_name',ASCENDING)])
{u'status': u'unprocessed', u'file_name':
u'2011-09-30_10:48:23.843037_46.137.255.217.log', u'_id':
ObjectId('4ee805981d011c644f00916c'), u'end_time':
datetime.datetime(2011, 12, 14, 2, 35, 52, 160000)}

Zac Witte

unread,
Dec 14, 2011, 9:37:01 PM12/14/11
to mongodb-user
I should maybe clarify - the code I pasted shows me doing the
following:


1) find_and_modify while sorting by file_name and it returning a
file_name starting with 2011-12-03
2) a normal find with the same query, but without the sort returning
the same file_name starting with 2011-12-03
3) a find with the same query and WITH the sort and returning
2011-09-30.

2011-09-30 is what SHOULD be returned when sorting, but 2011-12-03 is
what is returned without sorting. 2011-12-03 is what is returned
during a find_and_modify (or update) even though I include the sort
parameter. Am I doing the sort wrong or could there be some other
issue involved?

Thanks,

Nat

unread,
Dec 14, 2011, 10:14:20 PM12/14/11
to mongod...@googlegroups.com
- What mongod version? If it's 1.6.x, I remember that there was a bug related to that
- Do you have an index on filename, status?
- Can you do explain() on the query?

Bernie Hackett

unread,
Dec 14, 2011, 11:48:35 PM12/14/11
to mongodb-user
sort={'file_name': ASCENDING} will work.

On Dec 14, 4:21 pm, Zac Witte <zacwi...@gmail.com> wrote:

Zac Witte

unread,
Dec 15, 2011, 12:57:38 AM12/15/11
to mongodb-user
Thanks Bernie, that did the trick. I think the documentation could use
some fixing, since there's no indication of a different syntax from
the find command and there's no OperationFailure thrown when the wrong
parameter is supplied.

Zac

Nat

unread,
Dec 15, 2011, 1:18:00 AM12/15/11
to mongod...@googlegroups.com
I think it should take key_or_list just like find() since the order of the sorting actually matters, right? Especially, if you have more than one sorting order.

Bernie Hackett

unread,
Sep 26, 2012, 7:39:31 AM9/26/12
to mongod...@googlegroups.com
You can use either son.SON (supported back to python 2.4) or
collections.OrderedDict for this.

http://api.mongodb.org/python/current/api/bson/son.html#bson.son.SON

http://docs.python.org/library/collections.html#collections.OrderedDict

On Tue, Sep 25, 2012 at 10:18 PM, Walter Woods <woods...@gmail.com> wrote:
> Just want to vote in support that this should be fixed to take
> key_or_list... after all, a dict in python can change sort after the
> definition, so something like { 'b': 1, 'a': -1 } would end up sorting on
> 'a' descending BEFORE 'b' ascending, which isn't what would be intended.
> --
> 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

aliane abdelouahab

unread,
Sep 26, 2012, 7:43:19 AM9/26/12
to mongodb-user
Reply all
Reply to author
Forward
0 new messages