Proper way to import json file to mongo
Docs note that:
This utility takes a single file that contains 1 JSON/CSV/TSV string per line and inserts it.
In the structure you are using -assuming the errors on the gist are fixed- you are essentially importing one document with only shops
field.
After breaking the data into separate shop docs, import using something like (shops being the collection name, makes more sense than using example
):
mongoimport -d test -c shops data.json
and then you can query like:
db.shops.find({"name":x,"categories.type":"shirts"})
There is a parameter --jsonArray
:
Accept import of data expressed with multiple MongoDB document within a single JSON array
Using this option you can feed it an array, so you only need to strip the outer object syntax i.e. everything at the beginning until and including "shops" :
, and the }
at the end.
Myself I use a little tool called jq that can extract the array from command line:
./jq '.shops' shops.json