Pymongo cursor limit(1) returns more than 1 result

随声附和 提交于 2019-12-06 19:46:14

问题


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?


回答1:


The docs says this about index access of a cursor:

Any limit previously applied to this cursor will be ignored.



来源:https://stackoverflow.com/questions/14712402/pymongo-cursor-limit1-returns-more-than-1-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!