Importing JSON Records into MongoDB with mongoimport

The following instructions assume that each line in the file to be imported contains a complete JSON record.  This is the file format created by the mongoexport utility, so the following should be effective when exporting records from one MongoDB instance and importing them into another.

If MongoDB is installed in the C:\MongoDB folder, and the file to be imported is located at C:\Data.json, the format of the mongoimport command is:

C:\>mongodb\bin\mongoimport ––host localhost:27017 ––db mydb ––collection docs < C:\Data.json

In this example, “mydb” is the name of the database and “docs” is the name of the collection into which the data will be inserted.  Note that “localhost:27017” is the default server and port for MongoDB

The database and collection will be created if they do not already exist.

Here is example output from the mongoimport command:

C:\>mongodb\bin\mongoimport ––host localhost:27017 ––db mydb ––collection docs < c:\data.json
connected to: localhost:27017
Tue Nov 19 08:58:06.001                         64300   21433/second
Tue Nov 19 08:58:09.002                         142800  23800/second
Tue Nov 19 08:58:12.001                         213300  23700/second
Tue Nov 19 08:58:15.001                         292900  24408/second
Tue Nov 19 08:58:18.003                         373400  24893/second
Tue Nov 19 08:58:19.454 check 9 414106
Tue Nov 19 08:58:19.455 imported 414106 objects

To confirm that the records are now in MongoDB, do the following:

  1. Start the Mongo shell by running “C:\mongodb\bin\mongo
  2. Issue a “show dbs” command to see the available databases.  “mydb” should be listed.
  3. Issue a “use mydb” command to switch to the “mydb” database
  4. Issue a “show collections” command to see the collections in the database.  “docs” should be listed.
  5. Issue a “db.docs.find()” command to see the records in the “docs” collection.  The first twenty records will be returned.  Type “it” to see additional records.

Here is an example of what the output from these commands should look like:

C:\>mongodb\bin\mongo
MongoDB shell version: 2.4.8
connecting to: test
> show dbs
local   0.078125GB
mydb    0.453125GB
> use mydb
switched to db mydb
> show collections
docs
system.indexes
> db.docs.find()
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1a7”), “leaf_num” : “0” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1a8”), “leaf_num” : “1” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1a9”), “leaf_num” : “2” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1aa”), “leaf_num” : “3” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1ab”), “leaf_num” : “4” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1ac”), “leaf_num” : “5” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1ad”), “leaf_num” : “6” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1ae”), “leaf_num” : “7” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1af”), “leaf_num” : “8” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b0”), “leaf_num” : “9” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b1”), “leaf_num” : “10” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b2”), “leaf_num” : “11” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b3”), “leaf_num” : “12” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b4”), “leaf_num” : “13” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b5”), “leaf_num” : “14” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b6”), “leaf_num” : “15” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b7”), “leaf_num” : “16” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b8”), “leaf_num” : “17” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1b9”), “leaf_num” : “18” }
{ “_id” : ObjectId(“528b7c7b594d11167ecdd1ba”), “leaf_num” : “19” }
Type “it” for more
>