I've been trying to use mongo with some data imported, but I'm not able to use it properly with my document description.
This is an example of the .json I import using mongoimport: https://gist.github.com/2917854
mongoimport -d test -c example data.json
I noticed that all my document it's imported to a unique object in spite of creating one of object for each shop.
That's why when I try to find a shop or anything I want to query, all the document is returned.
db.example.find({"shops.name":"x"})
I want to be able to query the db to obtain products by the id using dot notation something similar to:
db.example.find({"shops.name":"x","categories.type":"shirts","clothes.id":"1"}
The problem is that all the document is imported like a single object. The question is: How
do I need to import the object to obtain my desired result?
There is a parameter
--jsonArray
: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:
IMPORT FROM JSON
JSON format should be in this format. (Array of Objects)
IMPORT FROM CSV
More Info
https://docs.mongodb.com/getting-started/shell/import-data/
Docs note that:
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
):and then you can query like:
Importing a JSON
The command
mongoimport
allows us to import human readableJSON
in a specific database & a collection. To import aJSON
data in a specific database & a collection, typemongoimport -d databaseName -c collectionName jsonFileName.json