JediJesus
JediJesus

Reputation: 329

Return Big Data using PyMongo

I have to execute this query on my MongoDB using pyMongo:

db.solution1.aggregate([ 
  {
    $project:{
      _id:0
    }
  },
  {
    $group:{
      "_id":{
        vehicleid:"$vehicleid",
        date:"$date"
      },count:{'$sum':1}
    }
  }
         ])

This works but i guess that is too much data to be returned: import pymongo

connection_string = 'mongodb://localhost'
connection = pymongo.MongoClient(connection_string)
database = connection.solutions

pipe2 = [ 
  {
    '$project':{
      "_id":0
    }
  },
  {
    '$group':{
      "_id":{
        "vehicleid":"$vehicleid",
        "date":"$date"
      },'count':{'$sum':1}
    }
  }
         ]

result = database.solution1.aggregate(pipe2)
print(result)

Result: <pymongo.command_cursor.CommandCursor object at 0x03761B38>

Is there any way that i can return this documents?

Upvotes: 1

Views: 144

Answers (1)

briba
briba

Reputation: 2987

You should not print a cursor. Try this:

json = list(result)
print(json)

Or also:

for node in result:
    value = node.items()[0]
    ...

Reference: Aggregate query in mongo works, does not in Pymongo

Upvotes: 1

Related Questions