推荐学习书目
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 · Mar 21, 2020 via Android · 2496 views
    This topic created in 2265 days ago, the information mentioned may be changed or developed.
    大佬们好,python 的 pymongo 库能不能有与 mongoexport 相同的功能的脚本?
    或者有什么方法实现这个导出的功能呢?实现将 mongo 数据导出到 json 文件。
    我试了用 python 调用 os.system 方法,但是不好使。
    5 replies    2020-03-21 21:45:22 +08:00
    520Python
        1
    520Python  
    OP
       Mar 21, 2020 via Android
    翻了一遍 pymongo 的官方文档,没有找到 export 相关的资料。
    williamZXS
        2
    williamZXS  
       Mar 21, 2020
    有没有试过用 pymongo coll.find() 的得到数据后 json.dump() 写到 json 文件
    lithiumii
        3
    lithiumii  
       Mar 21, 2020
    cur = db.collection.find({})
    all_documents = list(cur)
    然后你就得到了一个 collection 里所有的文件,接下来 json.dump 就是了
    文件多到大于内存的话就 for doc in cur 然后接着写
    520Python
        4
    520Python  
    OP
       Mar 21, 2020 via Android
    @williamZXS 好的,谢谢,明天试下!
    520Python
        5
    520Python  
    OP
       Mar 21, 2020 via Android
    @lithiumii 谢谢你的解答,明天试试看。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3090 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 15:02 · PVG 23:02 · LAX 08:02 · JFK 11:02
    ♥ Do have faith in what you're doing.