Pymongo返回值仅作为列表 [英] Pymongo return values only as list

查看:101
本文介绍了Pymongo返回值仅作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下pymongo查询为我提供了我需要的所有值:

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'}

但是我如何告诉pymongo仅以列表形式(没有键)将值返回给我?

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

推荐答案

只需这样做:

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

然后,您可以使用传统的for循环或简单地使用光标.

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

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

请注意,此处Cursor对象包含一个文档,因为我们将None

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

这篇关于Pymongo返回值仅作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆