我想通过命令导出MongoDB中的所有集合:

mongoexport -d dbname -o Mongo.json

结果是: 没有指定集合!

手册说,如果你不指定一个集合,所有的集合都将被导出。 然而,为什么这行不通呢?

http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection

我的MongoDB版本是2.0.6。


当前回答

导出所有集合:

mongodump -d database_name -o directory_to_store_dumps

要恢复它们:

mongorestore -d database_name directory_backup_where_mongodb_tobe_restored

其他回答

如果你愿意,你可以将所有的集合导出到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;

如果您想连接远程mongoDB服务器(如mongolab.com),则应该传递连接凭据 如。

mongoexport -h id.mongolab.com:60599 -u username -p password -d mydb -c mycollection -o mybackup.json

我把所有的收集倾倒在robo3t上。 我在vagrant/homestead上运行下面的命令。这是我的工作

mongodump --host localhost --port 27017 --db db_name --out db_path

可以使用mongodb命令进行修改

步骤1:打开命令提示符 第二步:打开mongoDB安装的bin文件夹(C:\Program Files\ mongoDB \Server\4.0\bin) 步骤3:然后执行以下命令 mongodb -d your_db_name -o destination_path Your_db_name = test destination_path = C:\Users\HP\Desktop

导出的文件将创建在destination_path\your_db_name文件夹中(在本例中是C:\Users\HP\Desktop\test)

参考资料:o7planning

对于转储,您的DB使用下面的CMD命令

   mongodump -d <your d name> -o <dump path>
Ex:mongodump -d qualetics -o D:\dbpackup\qualetics