Failed Aggregation on Tornado/Motor: yielded unknown object MotorAggregationCursor

时光总嘲笑我的痴心妄想 提交于 2019-12-24 12:47:37

问题


I'm having issue to execute MongoDB aggregation operation on Tornado. This is the code,

pipeline = [
   {'$match': {
       '$or': [
           {'owner.id': '56dfdaa4082024b9384c0055'},
           {'members.top.member.id':'56dfdaa4082024b9384c0055'}
       ]
   }},
   {'$sort': {'date_s': -1}},
   {'$skip': 0},
   {'$limit': 20},
   {'$project':{
      'created_at': 1,
      'name': 1,
      'id': '$_id',
      'group.group_id': 1,
      '_id': 0,
      'permission': 1,
      'owner': 1,
      'type': 1,
      'members.total': 1,
      'desc': 1,
      'declared': 1
  }}
]
cursor = yield db.activities.aggregate(pipeline)

The same command runs perfectly well on MongoDB management tool (I'm using MongoChef). But on the Python Tornado, using "yield" async operation, it throws exception as

yielded unknown object MotorAggregationCursor(<motor.core._LatentCursor object at 0x00000000042DEA58>)

any idea? I'm short of clue for further debug... thank you


回答1:


The actual .aggregate() method is not itself "async". But the cursor iteration is.

So instead:

cursor = db.activities.aggregate(pipeline)
while (yield cursor.fetch_next):
    doc = cursor.next_object()
    print(doc)

Just like the docs say.



来源:https://stackoverflow.com/questions/36097756/failed-aggregation-on-tornado-motor-yielded-unknown-object-motoraggregationcurs

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