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)
mongoexport command is used to export MongoDB Collection data to a CSV or JSON file. By default, the mongoexport command connects to mongod instance running on the localhost port number 27017. Field_name(s) - Name of the Field (or multiple fields separated by comma (,) ) to be exported.
Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON. The following are some example documents (in JavaScript / Python style syntax) and their corresponding BSON representations.
If you want to modify your JSON data inside the data store, though, MongoDB will work better — it has tools for updating individual fields. To modify JSON fields in Postgres, on the other hand, you'd need to extract the whole document and then rewrite it back in when you've made your changes.
Modeling embedded data With MongoDB, this problem can easily be solved by embedding data using JSON as it supports embedded files. It can embed related data and lists in the same document instead of creating a new table as shown below. We just structured our JSON data for use in MongoDB.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With