Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb, pymongo, aggregate gives strange output (something about cursor)

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 ?

like image 929
hmmmbob Avatar asked May 19 '15 18:05

hmmmbob


1 Answers

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.

like image 79
bagrat Avatar answered Nov 18 '22 11:11

bagrat