如果我转储这样的数据库:
mongodump --archive=out.mdb
有没有办法将out.mdb
转换为平面JSON文件?如果是这样,怎么样? (例如,如果我只想恢复单个文档)
答案 0 :(得分:4)
我相信唯一的方法是使用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.