How to convert MongoDB archive file to JSON format?
Asked Answered
M

2

7

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)

Mellow answered 29/10, 2016 at 1:4 Comment(0)
U
5

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.

Umbilical answered 29/10, 2016 at 1:20 Comment(1)
Thanks - I appreciate the answer, though it seems unrelated to the original question which was about converting Mongo's binary archive format to JSON. Though your answer (which highlights the warning about JSON not representing the full data set) indicates it may be a bad idea to do this anyway.. so I think the ideal process would be to restore the archive file a temporary database and copy whatever document I need to the production database.Mellow
P
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.

Protection answered 26/5, 2017 at 11:8 Comment(1)
Thanks - I appreciate the answer, though it seems unrelated to the original question which was about converting Mongo's binary archive format to JSON.Ginter

© 2022 - 2024 — McMap. All rights reserved.