-->

Failed Aggregation on Tornado/Motor: yielded unkno

2019-07-28 21:18发布

问题:

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.