Subscribe via RSS Feed

How to Import and Export Through Mongodb?

July 8, 2014 0 Comments

Introduction:

Although while using mongodb we can create database and manipulate that same database. In real time we might have to import database from an external resource. That external database may be of json, csv or tsv file. And in other hand we might have to export mongodb database as Json format.
In this article we are going to discuss about the import and export properties of mongodb.

Importing json file in mongodb:

Importing JSON data into MongoDB can be tricky. By default, monogoimport assumes a special structure for a file to import from: similar to JSON format except that only one document per line is allowed with no comma after each of them – something like:


{ name: "entry 1", desc: "This is entry 1" }
{ name: "entry 2", desc: "This is entry 2" }
{ name: "entry 3", desc: "This is entry 3" }

Although this is tricky to execute, we can use mongoimport to directly install a certain JSON file into mongodb. Let us assume that we have a JSON named test. This includes the above data in json format. Like this:


[
{ name: "entry 1", desc: "This is entry 1" },
{ name: "entry 2", desc: "This is entry 2" },
{ name: "entry 3", desc: "This is entry 3" }

]

We can import it to our local MongoDB database using –jsonArray parameter into a JSON array. Let us view the code to do so:


mongoimport --db --collection --type json --file test.json—jsonArray

For the above code to be functional you have to keep the JSON file in the same directory that you have selected in the terminal.
Except this mongodb also allows us to use many tools to import from json. One of them is jq. To use jq tool to extract the array from JSON you can use the following code.


./jq '.test' test.json

In other times we may have to import data from a bson file format. BSON is de­signed to be tra­versed eas­ily. This is a vi­tal prop­erty in its role as the primary data rep­res­ent­a­tion for MongoDB. So in this case applying mongorestore is enough. We just have to declare the path to mongorestore. The code is as follows:


mongorestore -d test /path/test

Importing a csv file:

In times of importing a certain csv file we can also use mongoimport as our importing tool. For this example going with the flow I have created a csv file containing data of Brazil world cup football team. You can have csv files as you like.
To import this file we can follow the below code:


mongoimport -h staff.mongohq.com --port 10097 -d import_example -c brazil_players -u mongohq -p****** --type csv --file ~/path/brazil_players.csv

If you briefly look over that command, you can see that we had to pass the host, port, database, collection, authentication, file type, name of file and some optional parameters to tell us about the characteristics of the file. Let us discuss below what we have used?

  • -h

We have used his parameter to specify our host name which is staff.mongohq.com.

  • –port

This parameter specifies the port on the host that the MongoDB database is listening on. Which are 10097. It is –port instead of -p to prevent confusion with the password parameter.

  • -d

It holds the name of your database.

  • -c

This indicates the collections of your database.

  • -u/-p

Since mongohq needs an authentication we need to provide with proper username and password respectively.

  • –type

This denotes the file type you are importing. In this example it is a csv.

  • –file

In this section we have to provide the file path.
If everything is ok then you’ll get output of every data that you have in your database. By saying this I also want to tell you that many times we have to disregard the first line of database for being that line a heading. If you confront a situation like this, you can use –headerline to disregard the first line.

So we have learned how to import JSON, bson and csv files. But there is another feature that we should talk about. You may have to export your mongodb database file also, depending upon the circumstances.
Bu not every time you can make that a mongodb default file. You also have to export your database as JSON or csv format. In this later section of the document let us see how can we achieve this?

Exporting as JSON file:

When we are working on mongodb. We might have to take our data from the server and use somewhere else. For this mongodb supplies us with many tools. One of them is mongodump. But in case if you use mongodump, you can’t specify the output format. To export the database in different format than bson we have to use mongoexport.
For exporting a data collection to a json you can use this simple command:


mongoexport -d Database name -c collection name -o output file format

In the above example we are using mongoexport. To use this tool we have to specify the database name, the collection name and the output format.
So, if we assume that the previous example of Brazil football team, the database is created with mongodb and now should exported as JSON format. Then we can do the following.


mongoexport -d export_example -c brazil_players -o brazil_players.json

Exporting as csv file:

As well as we have imported csv files in mongodb we may have to export files from mongodb as a csv file. Let us work over with the same database. Exporting as a csv file will be a little different than JSON.
Let us look at the code:


mongoexport -d export_example -c brazil_players -f name,age --csv -o brazil_players.csv

As we can see in the above code we have two different parameters than before.

  • -f

Indicates the fields that we are going to convert

  • –csv

Indicates that the files are converted into csv file format instead of JSON.

Summary:

In this article we have learnt a little about the importing and exporting in mongodb. When we are doing projects we have to face this kind of situation every now and then. So, everyone is trying to simplify the process as much as they can do.

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Get Started with MongoDB?
3> How to Use Projection in MongoDB?
4> Using sort method in mongodb
5> Map-Reduce in MongoDB
6> Introduction to Replication in MongoDB
7> Deploying a Replica Set in MongoDB
8> Discussing Replication Lag in MongoDB
9> Replica Set Members in Mongodb
10> Working with Sharding in MongoDB
11> Working with Index in MongoDB
12> Working with Aggregation in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

Leave a Reply

You must be logged in to post a comment.