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

mongoexport -d dbname -o Mongo.json

结果是: 没有指定集合!

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

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

我的MongoDB版本是2.0.6。


当前回答

可以使用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

其他回答

如果你愿意,你可以将所有的集合导出到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 /bin> mongoexport -d webmitta -c domain -o domain-k.json

进口:

mongoimport -d dbname -c newCollecionname——file domain-k.json

在哪里

webmitta(db name)
domain(Collection Name)
domain-k.json(output file name)

有多种选择,这取决于你想做什么

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

如果你有这样的问题: 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

通过这个命令导出整个Mongo DB

mongoexport –db database_name –collection collection_name –out path_or_filename.json