Pymongo:遍历集合中的所有文档 [英] Pymongo: iterate over all documents in the collection
问题描述
我正在使用PyMongo并尝试遍历MongoDB集合中的(一千万个)文档,并仅提取几个键:名称和地址,然后将它们输出到.csv文件。
I am using PyMongo and trying to iterate over (10 millions) documents in my MongoDB collection and just extract a couple of keys: "name" and "address", then output them to .csv file.
我无法找出使用find()。forEach()的正确语法
I cannot figure out the right syntax to do it with find().forEach()
我正在尝试类似
cursor = db.myCollection.find({"name": {$regex: REGEX}})
其中REGEX会匹配所有内容-并导致杀死。
我也尝试过
where REGEX would match everything - and it resulted in "Killed". I also tried
cursor = db.myCollection.find({"name": {"$exist": True}})
但这也不起作用。
有什么建议吗?
推荐答案
我找不到正确的语法来做到这一点与find()。forEach()
I cannot figure out the right syntax to do it with find().forEach()
cursor.forEach()不适用于Python,它是一个JavaScript函数。您将必须获得一个游标并对其进行迭代。请参见 PyMongo教程:查询多个文档,您可以在其中进行操作:
cursor.forEach() is not available for Python, it's a JavaScript function. You would have to get a cursor and iterate over it. See PyMongo Tutorial: querying for more than one document, where you can do :
for document in myCollection.find():
print(document) # iterate the cursor
其中REGEX将匹配所有内容-并且它导致被杀。
where REGEX would match everything - and it resulted in "Killed".
不幸的是,这里缺少调试为什么的原因和信息的信息。虽然如果您想匹配所有内容,则只需声明:
Unfortunately there's lack of information here to debug on why and what 'Killed' is. Although if you would like to match everything, you can just state:
cursor = db.myCollection.find({"name": {$regex: /.*/}})
给出该字段名称
包含字符串值。尽管使用 $ exists
检查字段 name
是否比使用正则表达式更可取。
Given that field name
contains string values. Although using $exists
to check whether field name
exists would be preferable than using regex.
使用 $ exists 运算符不正确。您在 $ exists
中丢失了 s
。同样,不幸的是,我们对很多无效信息的了解并不多,这些信息无法帮助进一步调试。
While the use of $exists operator in your example above is incorrect. You're missing an s
in $exists
. Again, unfortunately we don't know much information on what 'didn't work' meant to help debug further.
如果您正在编写用于Python练习的脚本,我建议您查看一下:
If you're writing this script for Python exercise, I would recommend to review:
- PyMongo Tutorial
- MongoDB Tutorial: query documents
您还可以在 MongoDB大学 M101P:适用于Python开发人员的MongoDB的a> 。
You could also enrol in a free online course at MongoDB University for M101P: MongoDB for Python Developers.
但是,如果您只是想完成从集合中导出CSV的任务。另外,您也可以使用MongoDB的 mongoexport 。
However, if you are just trying to accomplish your task of exporting CSV from a collection. As an alternative you could just use MongoDB's mongoexport. Which has the support for :
- 通过-字段名称,地址
- 通过-键入 csv
- 通过 --query ...
- Exporting specific fields via --fields "name,address"
- Exporting in CSV via --type "csv"
- Exporting specific values with query via --query "..."
请参见 mongoexport用法以获取更多信息。
See mongoexport usage for more information.
这篇关于Pymongo:遍历集合中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!