1

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

4

1 回答 1

2

实际.aggregate()方法本身并不是“异步”的。但是游标迭代是。

所以与其:

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

就像文档说的那样。

于 2016-03-19T04:36:46.347 回答