0

这不是一个重复的问题另一个问题与 Mongo 2.6 有关,它确实与 Mongo 2.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
}]

代码:

cursor = yield db[collection].aggregate(bsonQuery)
self.write(bson.json_util.dumps(cursor))

错误:

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

我还想指出,这是 Mongo 2.4 和 PyMongo 2.8。我知道一些有类似错误的人被告知要在cursor没有的情况下存储yield,然后执行while(yield...). 试过了,似乎不适用于 Mongo 2.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
4

1 回答 1

1

我实际上打算将此添加到它的表弟问题中,因为 MongoDB 2.4 可以专门抛出此错误还有另一个原因。

简单地说,MongoDB 2.4 不支持聚合结果中的“游标”。这意味着您需要专门“关闭”该选项,因此它会.aggregate()自行调用“异步”方法,并且返回的结果现在是包含可枚举数组的文档"results"

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

因此,cursor=False在调用中缺少它以使其“异步”。

于 2016-03-24T21:49:03.890 回答