Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pymongo cursor limit(1) returns more than 1 result

These are all documents in my collection:

{
    "_id" : ObjectId("5110291e6ee1c31d5b275d01"),
    "d" : 24,
    "s" : [
        1,
        2,
        3
    ]
}
{
    "_id" : ObjectId("511029266ee1c31d5b275d02"),
    "d" : 24,
    "s" : [
        4,
        5,
        6
    ]
}
{
    "_id" : ObjectId("5110292e6ee1c31d5b275d03"),
    "d" : 24,
    "s" : [
        7,
        8
    ]
}

This the query I want to run:

mongo = get_collection(self.collection_name)
res = mongo.find().sort([('_id', -1)]).skip(1).limit(1)

get_collection() is a helper method that I've made. Iterating over the cursor, res, produces only one document:

res = mongo.find().sort([('_id', -1)]).skip(1).limit(1)
for document in res:
    print document

> {u's': [4.0, 5.0, 6.0], u'_id': ObjectId('511029266ee1c31d5b275d02'), u'd': 24.0}

However, accessing res using offsets returns two different documents for the 0th and 1st element:

res = mongo.find().sort([('_id', -1)]).skip(1).limit(1)
pprint(res[0])
> {u'_id': ObjectId('511029266ee1c31d5b275d02'), u'd': 24.0, u's': [4.0, 5.0, 6.0]}
pprint(res[1])
> {u'_id': ObjectId('5110291e6ee1c31d5b275d01'), u'd': 24.0, u's': [1.0, 2.0, 3.0]}

Is this a bug? limit(1) should only return one result, no?

like image 379
skyler Avatar asked Feb 05 '13 16:02

skyler


1 Answers

The docs says this about index access of a cursor:

Any limit previously applied to this cursor will be ignored.

like image 179
schlamar Avatar answered Oct 14 '22 00:10

schlamar