简体   繁体   中英

how to apply aggregate in pymongo

Through the following query in MongoDb , I get all the teams from the entire data.

db.player_level_data.aggregate([{$group : {_id: {team_name : "$team_name"}}}]).toArray()

I need to import this array of teams in python through pymongo.I tried the following code.

pipe = [
{'$group': {'_id': {'team_name' : "$team_name"}}}
]
data  = db.player_level_data.aggregate(pipeline=pipe)
pprint (data)

But it printed a pymongo cursor object. Any ideas on this. Thanks

I assume cursor object is generator , so simply convert it to list and print that list:

pprint(list(data))

Note that data whould be empty after printing. so if you need to continue work on that data save list(data) to variable and interact with that variable:

l_data = list(data)
pprint(l_data)

Of course, this returns a cursor object.

list(cursor)

should get you a list of the items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM