How to export all collections in MongoDB?

前端 未结 28 1549
你的背包
你的背包 2020-11-29 14:32

I want to export all collections in MongoDB by the command:

mongoexport -d dbname -o Mongo.json

The result is:
No collection specifie

28条回答
  •  一向
    一向 (楼主)
    2020-11-29 14:44

    I needed the Windows batch script version. This thread was useful, so I thought I'd contribute my answer to it too.

    mongo "{YOUR SERVER}/{YOUR DATABASE}" --eval "rs.slaveOk();db.getCollectionNames()" --quiet>__collections.txt
    for /f %%a in ('type __collections.txt') do @set COLLECTIONS=%%a
    for %%a in (%COLLECTIONS%) do mongoexport --host {YOUR SERVER} --db {YOUR DATABASE} --collection %%a --out data\%%a.json
    del __collections.txt
    

    I had some issues using set /p COLLECTIONS=<__collections.txt, hence the convoluted for /f method.

提交回复
热议问题