问题
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)?
回答1:
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
来源:https://stackoverflow.com/questions/39312333/pymongo-return-values-only-as-list