如何在pymongo中返回一个mongodb对象数组(没有游标)? MapReduce可以做到吗? [英] How can I return an array of mongodb objects in pymongo (without a cursor)? Can MapReduce do this?

查看:75
本文介绍了如何在pymongo中返回一个mongodb对象数组(没有游标)? MapReduce可以做到吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pymongo中访问的mongo中设置了一个数据库.

I have a db set up in mongo that I'm accessing with pymongo.

我希望能够将一小部分字段放入字典列表中.所以,就像我键入时在mongo shell中得到的东西一样.

I'd like to be able to pull a small set of fields into a list of dictionaries. So, something like what I get in the mongo shell when I type...

db.find({},{"variable1_of_interest":1, "variable2_of_interest":1}).limit(2).pretty()

我想要一个类似python的语句:

I'd like a python statement like:

x = db.find({},{"variable1_of_interest":1, "variable2_of_interest":1})

其中x是某种数组结构而不是游标---而不是像这样进行迭代:

where x is an array structure of some kind rather than a cursor---that is, instead of iterating, like:

data = []
x = db.find({},{"variable1_of_interest":1, "variable2_of_interest":1})
for i in x:
    data.append(x)

是否有可能我可以使用MapReduce将其整合为单行?像

Is it possible that I could use MapReduce to bring this into a one-liner? Something like

db.find({},{"variable1_of_interest":1, "variable2_of_interest":1}).map_reduce(mapper, reducer, "data")

我打算将此数据集输出到R进行分析,但我想将IO集中在Python中.

I intend to output this dataset to R for some analysis, but I'd like concentrate the IO in Python.

推荐答案

您无需调用mapReduce,只需将光标变成这样的列表即可:

You don't need to call mapReduce, you just turn the cursor into a list like so:

>>> data = list(col.find({},{"a":1,"b":1,"_id":0}).limit(2))
>>> data
[{u'a': 1.0, u'b': 2.0}, {u'a': 2.0, u'b': 3.0}]

其中col是您的db.collection对象.

where col is your db.collection object.

但是请谨慎对待大型/大型结果,因为这一切都会加载到内存中.

But caution with large/huge result cause every thing is loaded into memory.

这篇关于如何在pymongo中返回一个mongodb对象数组(没有游标)? MapReduce可以做到吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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