How to export all collections in MongoDB?

前端 未结 28 1521
你的背包
你的背包 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 15:03

    You can use mongo --eval 'printjson(db.getCollectionNames())' to get the list of collections and then do a mongoexport on all of them. Here is an example in ruby

      out = `mongo  #{DB_HOST}/#{DB_NAME} --eval "printjson(db.getCollectionNames())"`
    
      collections = out.scan(/\".+\"/).map { |s| s.gsub('"', '') }
    
      collections.each do |collection|
        system "mongoexport --db #{DB_NAME}  --collection #{collection}  --host '#{DB_HOST}' --out #{collection}_dump"
      end
    

提交回复
热议问题