V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
520Python
V2EX  ›  Python

大佬帮忙看下: Pymongo 有什么方法可以实现与 mongoexport 相同的功能?

  •  
  •   520Python · 2020-03-21 19:25:35 +08:00 via Android · 1565 次点击
    这是一个创建于 1490 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大佬们好,python 的 pymongo 库能不能有与 mongoexport 相同的功能的脚本?
    或者有什么方法实现这个导出的功能呢?实现将 mongo 数据导出到 json 文件。
    我试了用 python 调用 os.system 方法,但是不好使。
    5 条回复    2020-03-21 21:45:22 +08:00
    520Python
        1
    520Python  
    OP
       2020-03-21 19:30:57 +08:00 via Android
    翻了一遍 pymongo 的官方文档,没有找到 export 相关的资料。
    williamZXS
        2
    williamZXS  
       2020-03-21 19:32:19 +08:00
    有没有试过用 pymongo coll.find() 的得到数据后 json.dump() 写到 json 文件
    lithiumii
        3
    lithiumii  
       2020-03-21 19:40:36 +08:00
    cur = db.collection.find({})
    all_documents = list(cur)
    然后你就得到了一个 collection 里所有的文件,接下来 json.dump 就是了
    文件多到大于内存的话就 for doc in cur 然后接着写
    520Python
        4
    520Python  
    OP
       2020-03-21 21:44:01 +08:00 via Android
    @williamZXS 好的,谢谢,明天试下!
    520Python
        5
    520Python  
    OP
       2020-03-21 21:45:22 +08:00 via Android
    @lithiumii 谢谢你的解答,明天试试看。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   918 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:34 · PVG 04:34 · LAX 13:34 · JFK 16:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.