我想通过命令导出MongoDB中的所有集合:
mongoexport -d dbname -o Mongo.json
结果是: 没有指定集合!
手册说,如果你不指定一个集合,所有的集合都将被导出。 然而,为什么这行不通呢?
http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection
我的MongoDB版本是2.0.6。
我想通过命令导出MongoDB中的所有集合:
mongoexport -d dbname -o Mongo.json
结果是: 没有指定集合!
手册说,如果你不指定一个集合,所有的集合都将被导出。 然而,为什么这行不通呢?
http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection
我的MongoDB版本是2.0.6。
当前回答
你可以使用mongo——eval 'printjson(db.getCollectionNames())'来获取集合列表 然后对它们都做一个mongoexport。 下面是一个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
其他回答
如果你有这样的问题: Failed:不能创建会话:不能连接到服务器:connection(): auth error: sasl conversation error: unable to authenticate using mechanism " sran - sha -1": (AuthenticationFailed)认证失败。
然后添加——authenticationDatabase admin
eg:
mongodb -h 192.168.20.30:27018——authenticationDatabase admin -u dbAdmin -p dbPassword -d dbName -o path/to/folder
如果你愿意,你可以将所有的集合导出到csv,而不需要指定——fields(将导出所有字段)。
从http://drzon.net/export-mongodb-collections-to-csv-without-specifying-fields/运行这个bash脚本
OIFS=$IFS;
IFS=",";
# fill in your details here
dbname=DBNAME
user=USERNAME
pass=PASSWORD
host=HOSTNAME:PORT
# first get all collections in the database
collections=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();db.getCollectionNames();"`;
collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`;
collectionArray=($collections);
# for each collection
for ((i=0; i<${#collectionArray[@]}; ++i));
do
echo 'exporting collection' ${collectionArray[$i]}
# get comma separated list of keys. do this by peeking into the first document in the collection and get his set of keys
keys=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`;
# now use mongoexport with the set of keys to export the collection to csv
mongoexport --host $host -u $user -p $pass -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;
done
IFS=$OIFS;
之前的答案解释得很好,我添加了我的答案,以帮助您处理远程密码保护数据库
mongodump --host xx.xxx.xx.xx --port 27017 --db your_db_name --username your_user_name --password your_password --out /target/folder/path
有多种选择,这取决于你想做什么
1)如果你想导出你的数据库到另一个mongo数据库,你应该使用mongodump。这将创建一个包含BSON文件的文件夹,其中包含JSON不具有的元数据。
mongodump
mongorestore --host mongodb1.example.net --port 37017 dump/
2)如果你想将你的数据库导出为JSON,你可以使用mongoexport,除非你必须一次收集一个(这是设计好的)。然而,我认为用mongodb导出整个数据库,然后转换为JSON是最简单的。
# -d is a valid option for both mongorestore and mongodump
mongodump -d <DATABASE_NAME>
for file in dump/*/*.bson; do bsondump $file > $file.json; done
首先,启动Mongo DB的路径为->
C:\Program Files\MongoDB\Server\3.2\bin,然后单击mongo .exe文件启动MongoDB服务器。
命令在Windows中导出
命令用于将Windows环境下的MongoDB数据库从“remote-server”导出到本地机器的C:/Users/Desktop/temp-文件夹下,并使用内部IP地址和端口。
C:\> mongodump --host remote_ip_address:27017 --db <db-name> -o C:/Users/Desktop/temp-folder
命令在Windows中导入
将Windows下的MongoDB数据库从本地机器目录C:/Users/Desktop/temp-folder/db-dir导入到“remote-server”
C:\> mongorestore --host=ip --port=27017 -d <db-name> C:/Users/Desktop/temp-folder/db-dir