pymongo find_and_modify not honoring sort?
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Zac Witte <zacwi... @gmail.com>
Date: Wed, 14 Dec 2011 16:21:44 -0800 (PST)
Local: Wed, Dec 14 2011 7:21 pm
Subject: pymongo find_and_modify not honoring sort?
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)}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Zac Witte <zacwi... @gmail.com>
Date: Wed, 14 Dec 2011 18:37:01 -0800 (PST)
Local: Wed, Dec 14 2011 9:37 pm
Subject: Re: pymongo find_and_modify not honoring sort?
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,
On Dec 14, 4:21 pm, Zac Witte <zacwi... @gmail.com> wrote:
> 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)}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Nat <nat.lu... @gmail.com>
Date: Wed, 14 Dec 2011 19:14:20 -0800 (PST)
Local: Wed, Dec 14 2011 10:14 pm
Subject: Re: pymongo find_and_modify not honoring sort?
- 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?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Bernie Hackett <ber... @10gen.com>
Date: Wed, 14 Dec 2011 20:48:35 -0800 (PST)
Local: Wed, Dec 14 2011 11:48 pm
Subject: Re: pymongo find_and_modify not honoring sort?
sort={'file_name': ASCENDING} will work. On Dec 14, 4:21 pm, Zac Witte <zacwi... @gmail.com> wrote:
> 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)}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Zac Witte <zacwi... @gmail.com>
Date: Wed, 14 Dec 2011 21:57:38 -0800 (PST)
Local: Thurs, Dec 15 2011 12:57 am
Subject: Re: pymongo find_and_modify not honoring sort?
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
On Dec 14, 8:48 pm, Bernie Hackett <ber... @10gen.com> wrote:
> sort={'file_name': ASCENDING} will work.
> On Dec 14, 4:21 pm, Zac Witte <zacwi... @gmail.com> wrote:
> > 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)}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Nat <nat.lu... @gmail.com>
Date: Wed, 14 Dec 2011 22:18:00 -0800 (PST)
Subject: Re: pymongo find_and_modify not honoring sort?
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.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Walter Woods <woodswal... @gmail.com>
Date: Tue, 25 Sep 2012 19:18:08 -0700 (PDT)
Local: Tues, Sep 25 2012 10:18 pm
Subject: Re: pymongo find_and_modify not honoring sort?
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.
On Wednesday, December 14, 2011 4:21:44 PM UTC-8, Zac Witte wrote:
> 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)}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Bernie Hackett <ber... @10gen.com>
Date: Wed, 26 Sep 2012 07:39:31 -0400
Local: Wed, Sep 26 2012 7:39 am
Subject: Re: [mongodb-user] Re: pymongo find_and_modify not honoring sort?
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 <woodswal
... @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.
> On Wednesday, December 14, 2011 4:21:44 PM UTC-8, Zac Witte wrote:
>> 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)}
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user+unsubscribe@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
aliane abdelouahab <alabdeloua... @gmail.com>
Date: Wed, 26 Sep 2012 04:43:19 -0700 (PDT)
Local: Wed, Sep 26 2012 7:43 am
Subject: Re: pymongo find_and_modify not honoring sort?
You must
Sign in before you can post messages.
You do not have the permission required to post.