Importing Information Into MongoDB
If you want to import information into MongoDB database, lets take an example to import the database of resturants. It's in json format and can be downloaded using wget:
wget https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
After download is complete, you should have the file called primer-datset.json in the current directory. Now let's import the data into new database called newdb and into collection called restaurants. Type the command to import:
sudo mongoimport --db newdb --collection restaurants --file primer-dataset.json
The result should look like this:
Output of mongoimport
2016-01-17T14:27:04.806-0500 connected to: localhost
2016-01-17T14:27:07.315-0500 imported 26000 documents
26000 documents have been imported as shown above. MongoDB will be created automatically now
Let's verify the import by connecting to the newly created MongoDB database called newdb
like this:
sudo mongo newdb
Now you are connected to newly created database instance called newdb
Count the documents in the restaurants collection with the command:
db.restaurants.count()
This command will show total number of imported documents 26000. For even better check you can select the first document from the restaurants collected in the following way:
db.restaurants.findOne()
The result should look like this:
Output of db.restaurants.findOne()
{
"_id" : ObjectId("569beb098106480d3ed99926"),
"address" : {
"building" : "1007",
"coord" : [
-73.856077,
40.848447
],
"street" : "Morris Park Ave",
"zipcode" : "10462"
},
"borough" : "Bronx",
"cuisine" : "Bakery",
"grades" : [
{
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
},
...
],
"name" : "Morris Park Bake Shop",
"restaurant_id" : "30075445"
}
To exit the MongoDB prompt, type exit
at the prompt:
exit
Now you will be exited from the command line prompt.
Thanks for reading the blog.
0 Comment(s)