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{"num":1,"status":"ok"} {"num":2,"status":"ok"} {"num":3,"status":"ok"} {"num":4,"status":"ok"}You can directly import it into db using
mongoimport --db <db> --collections <collection> --file sample.jsonThis is straight forward and works well. But some third party services provide data in the form of JSON array like this
[ {"num":1,"status":"ok"}, {"num":2,"status":"ok"}, {"num":3,"status":"ok"}, {"num":4,"status":"ok"}, ]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 --jsonArrayIf You want to export Your collection as a json dump, You can use mongoexport command
mongoexport --db <db> --collections <collection> --out sample.jsonThis 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 --jsonArrayIf You are lazy to type, You can use
mongoexport --d <db> --c <collection> --o sample.json --jsonArray
References:: mongoDB docs - mongoimport, mongoexport