MongoDB - Import Data
Use the mongoimport utility to import data into a MongoDB database.
MongoDB provides the mongoimport
utility that can be used to import JSON, CSV, or TSV files into a MongoDB database.
mongoimport
is located in the bin directory (eg, /mongodb/bin or wherever you installed it).
To import data, open a new Terminal/Command Prompt window and enter mongoimport
followed by parameters such as database name, collection name, source file name, etc.
If you find that you can't run mongoimport, be sure that you've either exited the mongo utility, or opened a new Terminal/Command Prompt window before running mongoexport, as it is a separate utility.
Import JSON File
Here's an example of running mongoimport
to import a JSON file.
You might remember that we previously used mongoexport to export the artists collection to a JSON file.
We later dropped the artists collection altogether.
Now, we'll import that collection back into our database.
Resulting message:
2016-07-12T13:34:04.904+0700 no collection specified 2016-07-12T13:34:04.905+0700 using filename 'artists' as collection 2016-07-12T13:34:04.911+0700 connected to: localhost 2016-07-12T13:34:04.968+0700 imported 13 documents
If you don't specify a collection name, a collection is created based on the name of the file (minus any extension).
Now, let's switch back to our mongo Terminal/Command Prompt window and retrieve the list of collections in our database:
Result:
artists musicians producers
Now we'll query our revived collection.
Result:
{ "_id" : 1, "artistname" : "AC/DC" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014d"), "artistname" : "The Kooks" } { "_id" : ObjectId("5780fbf948ef8c6b3ffb0149"), "artistname" : "The Tea Party" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014f"), "artistname" : "Gang of Four" } { "_id" : ObjectId("5781d7f248ef8c6b3ffb014e"), "artistname" : "Bastille" } { "_id" : ObjectId("5781c9ac48ef8c6b3ffb014a"), "artistname" : "Jorn Lande" } { "_id" : ObjectId("5781f85d48ef8c6b3ffb0150"), "artistname" : "Deep Purple", "albums" : [ { "album" : "Machine Head", "year" : 1972, "genre" : "Rock" }, { "album" : "Stormbringer", "year" : 1974, "genre" : "Rock" } ] } { "_id" : ObjectId("578214f048ef8c6b3ffb0159"), "artistname" : "Miles Davis", "albums" : [ { "album" : "Kind of Blue", "year" : 1959, "genre" : "Jazz" }, { "album" : "Bitches Brew", "year" : 1970, "genre" : "Jazz" } ] } { "_id" : ObjectId("578217c248ef8c6b3ffb015a"), "artistname" : "Robben Ford", "albums" : [ { "album" : "Bringing it Back Home", "year" : 2013, "genre" : "Blues" }, { "album" : "Talk to Your Daughter", "year" : 1988, "genre" : "Blues" } ] } { "_id" : 2, "artistname" : "Prince", "address" : { "street" : "Audubon Road", "city" : "Chanhassen", "state" : "Minnesota", "country" : "United States" } } { "_id" : 4, "artistname" : "Rush" } { "_id" : 3, "artistname" : "Moby", "albums" : [ { "album" : "Play", "year" : 1999, "genre" : "Electronica" }, { "album" : "Long Ambients 1: Calm. Sleep.", "year" : 2016, "genre" : "Ambient" } ] } { "_id" : ObjectId("578217c248ef8c6b3ffb015b"), "artistname" : "Snoop Dogg", "albums" : [ { "album" : "Tha Doggfather", "year" : 1996, "genre" : "Rap" }, { "album" : "Reincarnated", "year" : 2013, "genre" : "Reggae" } ] }
Specify a Collection Name
You can use the --collection
argument to provide a name of the collection that the data should go into.
Let's import another file, but this time, specify a collection name:
Resulting message:
2016-07-12T14:09:01.793+0700 connected to: localhost 2016-07-12T14:09:01.849+0700 imported 1 document
Now switch back to mongo and check the list of collections:
Resulting message:
artists jazz musicians producers
And finally, query the jazz collection:
Resulting message:
{ "_id" : ObjectId("578214f048ef8c6b3ffb0159"), "artistname" : "Miles Davis", "albums" : [ { "album" : "Kind of Blue", "year" : 1959, "genre" : "Jazz" }, { "album" : "Bitches Brew", "year" : 1970, "genre" : "Jazz" } ] }
Import CSV File
You can import a CSV file by using --type csv
.
If the CSV file has a header row, use --headerline
to tell mongoimport to use the first line to determine the name of the fields in the resulting document.
If the CSV file doesn't have a header row, use the --fields
parameter to set the field names.
With Header Row
Here's an example of importing a document with a header row.
The contents of the CSV file:
_id,albumname,artistname 1,Killers,"Iron Maiden" 2,Powerslave,"Iron Maiden" 12,"Somewhere in Time","Iron Maiden" 3,"Surfing with the Alien","Joe Satriani" 10,"Flying in a Blue Dream","Joe Satriani" 11,"Black Swans and Wormhole Wizards","Joe Satriani" 6,"Out of the Loop","Mr Percival" 7,"Suck on This",Primus 8,"Pork Soda",Primus 9,"Sailing the Seas of Cheese",Primus
Import the file:
Query the collection:
> db.catalog.find() { "_id" : 2, "albumname" : "Powerslave", "artistname" : "Iron Maiden" } { "_id" : 1, "albumname" : "Killers", "artistname" : "Iron Maiden" } { "_id" : 3, "albumname" : "Surfing with the Alien", "artistname" : "Joe Satriani" } { "_id" : 12, "albumname" : "Somewhere in Time", "artistname" : "Iron Maiden" } { "_id" : 10, "albumname" : "Flying in a Blue Dream", "artistname" : "Joe Satriani" } { "_id" : 6, "albumname" : "Out of the Loop", "artistname" : "Mr Percival" } { "_id" : 7, "albumname" : "Suck on This", "artistname" : "Primus" } { "_id" : 8, "albumname" : "Pork Soda", "artistname" : "Primus" } { "_id" : 11, "albumname" : "Black Swans and Wormhole Wizards", "artistname" : "Joe Satriani" } { "_id" : 9, "albumname" : "Sailing the Seas of Cheese", "artistname" : "Primus" }
Without Header Row
Here's another CSV file, but this one doesn't have a header row:
Mutt Lange, 1948 John Petrucci, 1967 DJ Shadow, 1972 George Clinton, 1941
Now we'll import it and specify the field names to use:
Query the collection:
> db.producers.find() { "_id" : 1, "name" : "Bob Rock" } { "_id" : ObjectId("5784a3a5dfad478c015f6b72"), "name" : "John Petrucci", "born" : 1967 } { "_id" : ObjectId("5784a3a5dfad478c015f6b73"), "name" : "Mutt Lange", "born" : 1948 } { "_id" : ObjectId("5784a3a5dfad478c015f6b74"), "name" : "George Clinton", "born" : 1941 } { "_id" : ObjectId("5784a3a5dfad478c015f6b75"), "name" : "DJ Shadow", "born" : 1972 }
You'll see that the ObjectId field has been automatically created and populated for us.
Also, we already had one document in this collection before we ran the import: { "_id" : 1, "name" : "Bob Rock" }. Therefore, you can see that the import has simply added to the collection (as opposed to replacing it and all its contents).
You can use the same method to import TSV files. Simply use --type tsv
.