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

mongoexport -d dbname -o Mongo.json

结果是: 没有指定集合!

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

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

我的MongoDB版本是2.0.6。


当前回答

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

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

其他回答

之前的答案解释得很好,我添加了我的答案,以帮助您处理远程密码保护数据库

mongodump --host xx.xxx.xx.xx --port 27017 --db your_db_name --username your_user_name --password your_password --out /target/folder/path

我把所有的收集倾倒在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

我为此写了bash脚本。只需使用2个参数(数据库名称,存储文件的dir)运行它。

#!/bin/bash

if [ ! $1 ]; then
        echo " Example of use: $0 database_name [dir_to_store]"
        exit 1
fi
db=$1
out_dir=$2
if [ ! $out_dir ]; then
        out_dir="./"
else
        mkdir -p $out_dir
fi

tmp_file="fadlfhsdofheinwvw.js"
echo "print('_ ' + db.getCollectionNames())" > $tmp_file
cols=`mongo $db $tmp_file | grep '_' | awk '{print $2}' | tr ',' ' '`
for c in $cols
do
    mongoexport -d $db -c $c -o "$out_dir/exp_${db}_${c}.json"
done
rm $tmp_file

我需要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方法。