python - 使用 PyMongo 将 JSON 分块保存
问题描述
我在 MongoDB 中有一个相当大的集合,我需要使用 PyMongo 将整个内容导出到 JSON。现在我只是在做:
import json
results = db.collection_name.find()
with open('collection-data.json', 'w') as f:
json.dump(list(results), f)
这最终导致内核崩溃,因为它耗尽了我所有的内存。有没有办法将集合保存在块中,这样我就不会一次检索所有数据?
解决方案
在你的 shell 中试试这个:
mongoexport --db <database-name> --collection <collection-name> --out output.json
推荐阅读
- wpf - 在填充缺失角的同时保留线条的扁平 LineCap 属性
- django - Django:带有collectsatic的Dockerfile错误
- c++ - 具有可变模板参数的成员函数的显式特化
- c++ - 有什么方法可以使 Visual Studio C++ 错误输出有用吗?
- php - 无法在 PHP 中打开某些 DBF 文件
- linux - 带有 exec 和 tee 的命令将输出同时重定向到文件和控制台
- mapbox - mapbox 中的嵌套表达式
- javascript - 将字符串总和转换为浮点问题
- sql-server - 如何选择日期和上一个日期
- excel - 如何将选定选项列表转换为所有可能选项的是/否值列