How to import json data into mongodb

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In previous tutorial, we created a mongodb database “firstdb” in mongolab.  In this tutorial, we will be importing JSON data into this mongodb.  The data will come from Wikipedia’s record of the high/low temperatures of various states.

1.  First we wrote this data into JSON format as linked here and explained here.  The first few records, I inputted all the data.  But the remaining records, I omitted some data such as the Celsius temperature and the dates.  But that is okay, because mongodb is schema-less and not not require all the data in all the records.

Anyways, we save this JSON format into a file highlowtemp.json.

2.  Then we log into mongolab to determine the import command string under “Tools -> Import/export” …

import command help

import command help

3. We open up a shell to run the command …

import command

import command

The -h flag is the hostname.  The -d is the database.  The -c is the collection to import into.  The -u and -p is the user and password to connect to the database.  And the “–file highlowtemp.json” is our import file.

4.  If you get the error “exception:BSON representation of supplied JSON is too large”, try adding the –jsonArray at the end of the command.  You can learn more about the mongoimport and its flags here.

The reason being is that our json file consists of one big array — an array of records.  While this is valid JSON, it may be too much for mongoimport.   Alternatively, you could have reformatted the json file to be not an array but as a series of object each object being on its own line without comma separation.  Just like how mongodb had exported it …

example exported records

example exported records

as was seen in our previous tutorial.  While this is technically not JSON, mongodb is fine with it and find it easier to work with.

Note that mongoimport is not intended to import full instance production backups.  As noted in the documentation “because they will not reliably capture data type information”

Use mongorestore instead.