Pymongo return values only as list

和自甴很熟 提交于 2019-12-23 03:37:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!