MongoDB 2.4聚合错误:生成未知对象MotorAggregationCurs

2024-05-23 10:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这不是一个重复的问题另一个问题与Mongo2.6有关,它与Mongo2.4的聚合有很大不同。我已经读过另一个问题,甚至在这个问题的最后一段提到了。在

首先,我对Mongo非常陌生,对PyMongo甚至更新。我正在处理一个现有的脚本,并试图调试它为什么不能在本地运行。以下查询导致错误。在

查询:

[{
  u'$match': {
    u'geocode.co_iso2': u'US',
    u'brand._id': UUID('xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
  }
},
{
  u'$group': {
    u'_id': u'$brand._id',
    u'num': {u'$sum': 1}
  }
},
{
  u'$sort': {u'num': -1}
},
{
  u'$limit': 100000
}]

代码:

^{pr2}$

错误:

Exception: <class 'tornado.gen.BadYieldError'>:yielded unknown object MotorAggregationCursor(<motor.core._LatentCursor object at 0x10a897b50>)

我还要指出,这是mongo2.4和pymongo2.8。我知道有些有类似错误的人被告知在存储cursor时不使用yield,然后执行while(yield...)。尝试过,似乎不适用于mongo2.4。上面写着:

Exception: <class 'pymongo.errors.OperationFailure'>:command SON([('aggregate', u'MyCollection'), ('pipeline', [{u'$match': {u'geocode.co_iso2': u'US', u'brand._id': UUID('xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')}}, {u'$group': {u'_id': u'$brand._id', u'num': {u'$sum': 1}}}, {u'$sort': {u'num': -1}}, {u'$limit': 100000}]), (u'cursor', {})]) on namespace mydatabase.$cmd failed: unrecognized field "cursor

Tags: iduuidmatch错误groupcursornumus
1条回答
网友
1楼 · 发布于 2024-05-23 10:39:12

我实际上是想把这个添加到它的cousin question中,因为MongoDB 2.4可以抛出这个错误还有另一个原因。在

MongoDB 2.4中不支持简单的“游标”作为聚合结果。这意味着您需要明确地关闭该选项,从而使.aggregate()成为一个“异步”方法调用本身,返回的结果现在是一个包含可枚举数组"results"的文档:

reply = yield collection.aggregate(pipeline,cursor=False)
for doc in reply['results']:
    print(doc)

所以调用中缺少cursor=False,使其成为“异步”的。在

相关问题 更多 >

    热门问题