Importing & Exporting JSON Data To mongoDB!

mongoimport imports JSON, CSV or TSV data to mongoDB. If You are importing JSON data from a file to db, the file should have only one document per line and there shouldn't be any delimiters at the end of file. If You have a file sample.json like this
You can directly import it into db using
mongoimport --db <db> --collections <collection> --file sample.json
This is straight forward and works well. But some third party services provide data in the form of JSON array like this
If You try to import it, it throws FailedToParse error. For this, You need to pass additional param jsonArray.
mongoimport --db <db> --collections <collection> --file sample.json --jsonArray
If You want to export Your collection as a json dump, You can use mongoexport command
mongoexport --db <db> --collections <collection> --out sample.json
This exports data into JSON format with one document in a row.

If You pass jsonArray as an optional parameter, it will export the collection as a jsonArray
mongoexport --db <db> --collections <collection> --out sample.json --jsonArray
If You are lazy to type, You can use
mongoexport --d <db> --c <collection> --o sample.json --jsonArray

References:: mongoDB docs - mongoimport, mongoexport

I am Chillar Anand. I daydream a lot and write about the things that interest me here. You can read more about this blog here.

See all articles

RSS Feed for the blog

Edit this page