Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import json file in mongodb?

Tags:

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"             }         }     } } } 
like image 242
arglee Avatar asked Aug 25 '13 06:08

arglee


People also ask

How do I import a JSON file into a database in MongoDB?

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.


2 Answers

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.

like image 90
S.D. Avatar answered Sep 25 '22 11:09

S.D.


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!

like image 28
Jas Avatar answered Sep 22 '22 11:09

Jas