Import a JSON File into MongoDB with mongoimport

When using MongoDB, you can use mongoimport to import documents into a collection. mongoimport is a command line utility that imports content from an Extended JSON, CSV, or TSV file. The import file could have been created by mongoexport or some other export utility.

This article presents examples of importing a JSON file into MongoDB.

Example

Suppose we have the following JSON file called pets.json:

{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }

The following command imports the JSON file into MongoDB:

mongoimport --db=PetHotel --file=pets.json

In this case, I didn’t specify a collection to import it into, so it created a collection with the same name of the file (pets).

Check the Results

Let’s take a look at the collection.

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }

We can see that the documents were imported as expected.

Specify the Collection Name

You can use the --collection (or -c) parameter to specify a collection to import the file into.

Here’s an example of using the --collection parameter to import the same file into a different collection:

mongoimport --db=PetHotel --collection=pets2 --file=pets.json

If the collection doesn’t already exist, it will be created. If it already exists, then the import outcome will depend on the mode you’re using (more on this below).

Drop the Collection Before Importing

You can use the --drop parameter to drop any existing collection with the same name as the one you’re trying to create/import.

Imagine we have a second file, called pets2.json, with the following document:

{ "_id" : 4, "name" : "Bubbles" }
{ "_id" : 5, "name" : "Hop", "type": "Kangaroo" }

Here’s what happens if I import that document to the pets collection using the --drop option:

mongoimport --db=PetHotel --collection=pets --drop --file=pets2.json

Output:

2021-01-03T15:42:25.626+1000	connected to: mongodb://localhost/
2021-01-03T15:42:25.640+1000	dropping: PetHotel.pets
2021-01-03T15:42:25.734+1000	2 document(s) imported successfully. 0 document(s) failed to import.

The message tells us that the collection was dropped, and one document was imported.

Let’s take a look at the collection:

db.pets.find()

Result:

{ "_id" : 5, "name" : "Hop", "type" : "Kangaroo" }
{ "_id" : 4, "name" : "Bubbles" }

As expected, our two new documents are the only ones in the collection.

Import Modes

There are various import modes that you can use with mongoimport. These modes determine what happens if there’s already matching documents in the collection that you’re trying to import into.

The modes are as follows:

ModeDescription
insertThis is the default mode. This mode inserts the documents from the import file. If a matching document already exists in the collection, an error occurs. A matching document is one that has the same unique ID (such as a matching _id field) as a document in the import file.
upsertReplaces existing documents in the database with matching documents from the import file. All other documents are inserted.
mergeMerges existing documents that match a document in the import file with the new document. All other documents are inserted.
deleteDeletes existing documents in the database that match a document in the import file. Any non-matching documents have no effect.

See The Import Modes of mongoimport for examples of each mode.

Check for mongoimport

mongoimport is part of the MongoDB Database Tools package. The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.

If you’re not sure whether you have the MongoDB Database Tools/mongoimport installed, try running the following command in your Terminal or Command Prompt to check:

mongoimport --version

If you have it, you should see version information, etc. If you don’t have it, you can use the installation instructions over at the MongoDB website to install it on to your system.

Where to Run the Commands?

Don’t forget, you need to run mongoimport commands from your system’s command line (e.g. a new Terminal or Command Prompt window).

Don’t run them from the mongo shell.