11

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?

skyler
  • 8,010
  • 15
  • 46
  • 69

1 Answers1

14

The docs says this about index access of a cursor:

Any limit previously applied to this cursor will be ignored.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
schlamar
  • 9,238
  • 3
  • 38
  • 76