我想通过命令导出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。
当前回答
#mongodump using sh script
#!/bin/bash
TIMESTAMP=`date +%F-%H%M`
APP_NAME="folder_name"
BACKUPS_DIR="/xxxx/tst_file_bcup/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
/usr/bin/mongodump -h 127.0.0.1 -d <dbname> -o $BACKUPS_DIR/$APP_NAME/$BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUPS_DIR/$APP_NAME/$BACKUP_NAME
rm -rf /home/wowza_analytics_bcup/wowza_analytics/wowza_analytics
### 7 days old backup delete automaticaly using given command
find /home/wowza_analytics_bcup/wowza_analytics/ -mindepth 1 -mtime +7 -delete
其他回答
在尝试了许多复杂的例子后,我发现非常简单的方法对我有用。
我只是想从本地转储一个db,并将它导入到一个远程实例:
在本地机器:
mongodump -d databasename
然后我scp了我的转储到我的服务器机器:
scp -r dump user@xx.xxx.xxx.xxx:~
然后从转储的父目录简单地:
mongorestore
然后导入数据库。
当然,假设mongodb服务正在运行。
如果你愿意,你可以将所有的集合导出到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
mongoexport –db database_name –collection collection_name –out path_or_filename.json
我需要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方法。
你可以使用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