If I dump a database like so:
mongodump --archive=out.mdb
Is there some way to convert out.mdb
to a flat JSON file? If so, how? (for example, if I just wanted to restore a single document)
If I dump a database like so:
mongodump --archive=out.mdb
Is there some way to convert out.mdb
to a flat JSON file? If so, how? (for example, if I just wanted to restore a single document)
I believe the only way is from the database, using mongoexport
:
mongoexport --db yourdb -c yourCollection --out yourCollection.json
Just take notice of the following (from their website):
WARNING
Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.
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.
© 2022 - 2024 — McMap. All rights reserved.