我想通过命令导出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。
当前回答
如果你愿意,你可以将所有的集合导出到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;
其他回答
首先,启动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
对于懒人,使用mongodb,它更快:
mongodump -d <database_name> -o <directory_backup>
然后“恢复/导入”它(从directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>
这样,您就不需要单独处理所有集合。只需指定数据库。
注意,我不建议使用mongodb /mongorestore存储大数据。它非常慢,一旦超过10/20GB的数据,可能需要几个小时才能恢复。
如果你有这样的问题: 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
我需要Windows批处理脚本版本。这个帖子很有用,所以我想我也会把我的答案贡献给它。
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
我在使用set /p COLLECTIONS=<__collections.txt时有一些问题,因此使用了复杂的for /f方法。
如果您想连接远程mongoDB服务器(如mongolab.com),则应该传递连接凭据 如。
mongoexport -h id.mongolab.com:60599 -u username -p password -d mydb -c mycollection -o mybackup.json