i am new to mongodb database . i a creating an application in which data store as a json file. what i want to do is import that data into mongodb and then display it on command prompt. i already tried following commands but error is displaying Failure parsing josn near: }
my command is:
mongoimport -d mydb -c mydb --type json --file glossary.json --headerline
i put json file in c:\mongodb\bin\glossary.json thanx i advace.
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }
To import JSON file you need to follow the following steps: Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don't close this cmd to stay connected to the server. Step 2: Open another command prompt and run the mongo shell. Using the mongo command.
Your JSON seems to have only a single object. Format like {..},{..}
is expected.
So, use --jsonArray
option:
mongoimport -d mydb -c mycollection --jsonArray < glossary.json
The other option is to format the source document as mongodb expects it to be. This will make loading much faster.
If you have a large collection, --jsonArray
takes a very long time to import. I was getting about 10 documents/second. If you have a properly formatted .json
file you can just use --file
instead. I'm getting about 6000 documents/second now.
Good thing I didn't settle for the first option!
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