如何将MongoDB归档文件转换为JSON格式?

时间:2016-10-29 01:04:56

标签: mongodb mongodump

如果我转储这样的数据库:

mongodump --archive=out.mdb

有没有办法将out.mdb转换为平面JSON文件?如果是这样,怎么样? (例如,如果我只想恢复单个文档)

2 个答案:

答案 0 :(得分:4)

mongoexport

相信唯一的方法是使用mongoexport来自数据库:

mongoexport --db yourdb -c yourCollection --out yourCollection.json
来自mongo的

警告

请注意以下事项(来自website):

  

警告

     

避免使用mongoimport和mongoexport作为完整实例   生产备份。它们不能可靠地保留所有丰富的BSON数据   类型,因为JSON只能表示支持的类型的子集   由BSON。使用MongoDB Backup中描述的mongodump和mongorestore   这种功能的方法。

答案 1 :(得分:1)

    Command for export :


mongoexport --db <dbname> --collection <collectionname> --out collection.json
    Once exported, if there are BSON data is there, we can follow the below methods

Official MongoDB Java Driver comes with **utility methods for parsing JSON to BSON and serializing BSON to JSON**.
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
DBObject dbObj = ... ;
String json = JSON.serialize( dbObj );
DBObject bson = ( DBObject ) JSON.parse( json );
The driver can be found here: https://mongodb.github.io/mongo-java-driver/
    In this way, if we take, we can convert easily.