Tuesday, November 5, 2013

Importing data into a MongoDB database using mongoimport from a csv file

I want to import some data from a xml spreadsheet, and load it into a MongoDB database. To do that, the first step is to convert the xml data into a csv file, and then load that data into a MongoDB database using mongoimport.

Below I tried to import a simple csv file into a mongodb database using mongoimport.

Here is my sample csv file:

[root@isvx7 mongodb_new]# cat projects.csv
ISV Name,Storage Brand,Project,Owner
Oracle,SVC,SVC Chubby nodes and Oracle,Mayur
Oracle,IBM Tape,Oracle backup,Shashank
Oracle,V7000,Oracle 12c on V7000,Mayur
Oracle,DS8870,Easy Tier 5 for Oracle,Mayur


The version of MongoDB that I'm currently running

[root@isvx7 mongodb_new]# mongod -version
db version v2.4.8
Tue Nov  5 16:30:37.367 git version: a350fc38922fbda2cec8d5dd842237b904eafc14

Executing the mongoimport command on the same host running the mongodb server.

[root@isvx7 mongodb_new]# mongoimport --db mydb --collection projects --type csv --file ./projects.csv --headerline
connected to: 127.0.0.1
Tue Nov  5 15:47:08.606 imported 4 objects

As seen on the mongo shell

> show dbs
mydb    0.203125GB
> use mydb
switched to db mydb
> db.projects.find().toArray()
[
        {
                "_id" : ObjectId("5279756a601c0948fdb32f70"),
                "ISV Name" : "Oracle",
                "Storage Brand" : "SVC",
                "Project" : "SVC Chubby nodes and Oracle",
                "Owner" : "Mayur"
        },
        {
                "_id" : ObjectId("5279756c601c0948fdb32f71"),
                "ISV Name" : "Oracle",
                "Storage Brand" : "IBM Tape",
                "Project" : "Oracle backup",
                "Owner" : "Shashank"
        },
        {
                "_id" : ObjectId("5279756c601c0948fdb32f72"),
                "ISV Name" : "Oracle",
                "Storage Brand" : "V7000",
                "Project" : "Oracle 12c on V7000",
                "Owner" : "Mayur"
        },
        {
                "_id" : ObjectId("5279756c601c0948fdb32f73"),
                "ISV Name" : "Oracle",
                "Storage Brand" : "DS8870",
                "Project" : "Easy Tier 5 for Oracle",
                "Owner" : "Mayur"
        }
]
>

No comments: