Nickpick
Nickpick

Reputation: 6587

Pymongo return values only as list

The following pymongo query gives me all the values I need:

l=list(db.rounds.find({"current_strategy":"PPStrategy4016"},{"myFundsChange":1,"_id": 0}))

{'myFundsChange': '-0.30000000000000004'}, {'myFundsChange': '0.0'}, {'myFundsChange': '0.0'}, {'myFundsChange': '-0.040000000000000036'}, {'myFundsChange': '-0.08000000000000007'}, {'myFundsChange': '-0.20999999999999996'}, {'myFundsChange': '-0.47'}, {'myFundsChange': '0.0'},  {'myFundsChange': '0.0'}, {'myFundsChange': '-0.040000000000000036'}, {'myFundsChange': '-0.040000000000000036'}

But how can I tell pymongo to return me the values only as a list (without the key)?

Upvotes: 2

Views: 1969

Answers (1)

Sede
Sede

Reputation: 61225

Simply do it like this:

cursor = collection.aggregate([
    {"$match": {"current_strategy": "PPStrategy4016"}},
    {"$group": {
        "_id": None, 
        "myFundsChange": {"$push": "$myFundsChange"}
    }}
])

Then you can consume the cursor using a traditional for loop or simply.

for res in cursor:
    # do something with the result.

Note that the Cursor object contains one document here because we group by None

Upvotes: 3

Related Questions