【问题标题】:MongoDB 2.4 Aggregate Error: yielded unknown object MotorAggregationCursorMongoDB 2.4 聚合错误:产生未知对象 MotorAggregationCursor
【发布时间】:2016-03-24 09:02:15
【问题描述】:

这不是一个重复的问题 另一个问题与 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

【问题讨论】:

    标签: python mongodb mongodb-query aggregation-framework tornado-motor


    【解决方案1】:

    我实际上是想把它添加到它的 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 以使其“异步”。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 2015-08-27
      • 2018-09-21
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多