ffxsam
ffxsam

Reputation: 27733

How to convert MongoDB archive file to JSON format?

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)

Upvotes: 7

Views: 4850

Answers (2)

priya raj
priya raj

Reputation: 362

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.

Upvotes: 1

alexbt
alexbt

Reputation: 17055

mongoexport

I believe the only way is from the database, using mongoexport:

mongoexport --db yourdb -c yourCollection --out yourCollection.json

warning from mongo

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.

Upvotes: 5

Related Questions