mongodb, pymongo, aggregate gives strange output (

2019-04-05 16:24发布

I am trying get a list of people with the most entries in my database.

print db.points.aggregate(
   [
      {
         "$group":
                    {
                       "_id": "$created.user", 
                       "count":{"$sum":1}
                    }
      },
      {
         "$sort":
                   {"count":-1}
      }
   ]
)

An entry looks like this :

{
   u'id': u'342902', 
   u'_id': ObjectId('555af76a029d3b1b0ff9a4be'), 
   u'type': u'node', 
   u'pos': [48.9979746, 8.3719741], 
   u'created': {
                  u'changeset': u'7105928', 
                  u'version': u'4', 
                  u'uid': u'163673', 
                  u'timestamp': u'2011-01-27T18:05:54Z', 
                  u'user': u'Free_Jan'
               }
}

I know that created.user exists and is otherwise accessible.

Still the output i get is:

<pymongo.command_cursor.CommandCursor object at 0x02ADD6B0>

Shouldn't I get a sorted list ?

1条回答
干净又极端
2楼-- · 2019-04-05 16:59

The result of an aggregation query is a cursor, as for a regular find query. In case of pymongo the CommandCursor is iterable, thus you are able to do any of the following:

cursor = db.points.aggregate(...)

# Option 1
print(list(cursor))

# Option 2
for document in cursor:
    print(document)

Note: as arun noticed, in both cases, i.e. after you create a list out of the cursor, or iterate in the for loop, you will not be able to re-iterate over the cursor. In that case the first option becomes better, if you want to use it in future, as you can use the obtained list as much as you want, because it is in the memory already.
The reason of not being able to reiterate is that the cursor is actually on the server, and it send the data chunk-by-chunk, and after it has sent you all the data (or the server terminates) the cursor gets destroyed.

查看更多
登录 后发表回答