top of page

How Import CSV file in Mongodb using MongoDB shell ? | How to add field and fetch record ?

Updated: Jun 12, 2023


In the previous blog we discussed how to install MongoDB and this blog we use some query to import csv file and perform operation how to fetch record.



How to import csv file:


Step 1

Go to the directory where MongoDB is kept.

Step 2

Go to its Bin folder.

Step 3

Save or keep your CSV or TXT file here which you want to import. The image below shows exactly what you need to do.

This is our CSV file named contrib_data.csv or any other name.

The top word is the headerline which will be used in the command line MongoDB import command later.

Step 4

Now into the same folder open the cmd by pressing ctrl+L and typing cmd there. Press enter and you will see a cmd window now. (You can open this cmd window at this place by right-clicking the mouse along with shift key pressed – here you will see open command window here)

Step 5

Type the undermentioned command: Import csv file in MongoDB shell:

mongoimport --db MongoDB --collection Bank_data --type csv --file Bank_data.csv --headerline

Press enter And you will see that your document data will be imported.

After this go to the MongoDB shell and than start:


> mongo

> show dbs

> use MongoDB

> show collections

>db.Bank_data.findOne()

Here some example to handle database:

Example1:

What is the count of records where the transaction_amt is less than $10?

syntax:


>db.collection_name.find(field_name:{$lt:given_conditional_data}}.count()
 
>db.Bank_data.find( { TRANSACTION_AMT: { $lt: 10 } } ).count()

Example2:

Add a new field named "donar_name" to the collection "Bank_data". donar_name field should have a value of "naveen" if the transaction_amt is greater than or equal to $500 and "sachin" if transaction_amt is less than $500. Print out total amount for each donar_name as final output.


>db.Bank_data.update({},

   {

     $set: { "donar_name" : ""
 

        }

   },false,true

)
 
 
>db.Bank_data.updateMany(

   { "TRANSACTION_AMT": { $gte: 500 } },

   {

     $set: { "donar_name" : "naveen"}

   }

)
 
 
>db.Bank_data.updateMany(

   { "TRANSACTION_AMT": { $lt: 500 } },

   {

     $set: { "donar_name" : "sachin"}

   }

)
 
 
>db.Bank_data.aggregate(

   [

     {

       $group:

         {

           _id : {donar_name :"$donar_name"},

           totalAmount: { $sum: "$TRANSACTION_AMT"}

         }

     }

   ]

)
 

Example 3:

Give the top 5 cmte_received by transaction amount.


>db.Bank_data.aggregate(
[ 
  { $sort : { TRANSACTION_AMT:-1} }, 
  { $limit : 5 }, 
  { $project : {_id: 0,cmte_received : 1,TRANSACTION_AMT: 1} } 
]
)

Example 4:

Give the top 5 cmte_received by transaction amount that candidate 'Aanand, arvind' gave money to.



>db.Bank_data.aggregate([

    { $sort : { TRANSACTION_AMT:-1} },

    { $match : { "CAND_NAME" : "Aanand, arvind"} },
    { $project : {_id: 0,cmte_received  : 1,CAND_NAME: 1,TRANSACTION_AMT:1} },

      { $limit : 5 }

   ]

)

For more details about MongoDB and Assignment help or homework help or a any project assignment help contact here

If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.

bottom of page