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 CSV file into MongoDB.
Example
Suppose we have the following CSV file called pets.csv
:
_id,name,type 1,"Wag","Dog" 2,"Bark","Dog" 3,"Meow","Cat"
The following command imports the CSV file into MongoDB:
mongoimport --db=PetHotel --type=csv --headerline --file=pets.csv
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.
CSV Files Without Column Headers
In the previous example, we used the --headerline
parameter to specify that the first line should be used for the field names.
If your CSV file doesn’t contain a header line, you’ll need to use either the --fields
parameter or the --fieldFile
parameter to specify the field names.
So, imagine our CSV file didn’t have the column line, and looked like this instead:
1,"Wag","Dog" 2,"Bark","Dog" 3,"Meow","Cat"
We could then use the following command to import the file:
mongoimport --db=PetHotel --type=csv --fields=_id,name,type --file=pets.csv
That has the same result as the previous example.
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 --type=csv --fields=_id,name,type --file=pets.csv
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 later).
In our case, here’s what the newly created collection looks like:
db.pets2.find()
Result:
{ "_id" : 2, "name" : "Bark", "type" : "Dog" } { "_id" : 3, "name" : "Meow", "type" : "Cat" } { "_id" : 1, "name" : "Wag", "type" : "Dog" }
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.csv
, with the following document:
_id,name,type,weight 4,"Bubbles","Fish",3
Here’s what happens if I import that document to the pets2
collection using the --drop
option:
mongoimport --db=PetHotel --collection=pets2 --type=csv --headerline --drop --file=pets2.csv
Output:
2021-01-03T15:05:40.281+1000 connected to: mongodb://localhost/ 2021-01-03T15:05:40.284+1000 dropping: PetHotel.pets2 2021-01-03T15:05:40.336+1000 1 document(s) imported successfully. 0 document(s) failed to import.
This tells us that the collection was dropped, and one document was imported.
Let’s take a look at the collection:
db.pets2.find()
Result:
{ "_id" : 4, "name" : "Bubbles", "type" : "Fish", "weight" : 3 }
As expected, our new document is the only one 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:
Mode | Description |
---|---|
insert | This 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. |
upsert | Replaces existing documents in the database with matching documents from the import file. All other documents are inserted. |
merge | Merges existing documents that match a document in the import file with the new document. All other documents are inserted. |
delete | Deletes 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 mongoimport
Commands?
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.