Pymongo游标限制(1)返回多个结果 [英] Pymongo cursor limit(1) returns more than 1 result

查看:280
本文介绍了Pymongo游标限制(1)返回多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都是我收藏中的所有文件:

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
    ]
}

这是我要运行的查询:

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

get_collection()是我制作的辅助方法.遍历光标res只会生成一个文档:

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}

但是,使用偏移量访问res返回第0个元素和第1个元素的两个不同文档:

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]}

这是一个错误吗? limit(1)应该只返回一个结果,不是吗?

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

推荐答案

The docs says this about index access of a cursor:

以前应用于此光标的任何限制都将被忽略.

Any limit previously applied to this cursor will be ignored.

这篇关于Pymongo游标限制(1)返回多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆