top of page

MongoDB Installation, Queries Practice Set | Hire MongoDB Expert

Requirement

Create a collection names "Contrib_data" using Contrib_data.csv file in canvas under a MongoDB database named assign.


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


2. Add a new field named "donar_cat" to the collection "Contrib_data".

donar_cat field should have a value of "big_fish" if the transaction_amt is greater than or equal to $500 and "small fish" if transaction_amt is less than $500. Print out total amount for each donar_cat as final output.


3. Give the top 5 cmte_received by transaction amount.


4. Give the top 5 cmte_received by transaction amount that candidate 'BACHMANN, MICHELE' gave money to.


Installation

First go to the MongoDB official website:


















Click Try free at right corner.



















And after this choose server button. Choose OS, and package MIS and start down load.



















Click on downloaded file and then Click next button


















Accept term and condition, and then click on next button.


Select Choose type: complete




































Click on next


















Again Click next


And then after installation is started


















After MongoDB installation successfully finish you click Finish button on next window.


Go to the Program files ->MongoDB->bin


Copy the path and goto the command prompt and paste the path as like and press enter














After this type mongod command and press Enter

Server installation started:

Some exception raise regarding path to create data->db inside C directory.


And after run mongod command to run server.

Open Another command promt.


And run again:

cd C:\Program Files\MongoDB\Server\4.0\bin 

press enter

run mongo command.

After this to show all database inside use :

>Show dbs 

command


Output: it exist default database inside

Admin 0.000GB
Config  0.000GB
Local 0.000GB

Create our database use command as:

>use naveen 

press enter

Switched to db Naveen

>Show dbs

Output

Admin 0.000GB
Config  0.000GB
Local 0.000GB

Not show naveen database because not inserted data so insert data use this command.


Syntax:

(db.name of collection.insert({“keyname”:”value”}))


Example:

>db.books.insert({“name”:”mongo book”})           

Press enter and create this collection:

Result:

WriteResult({“nInserted” : 1})

To show database use

>show dbs

Result:

Admin 0.000GB
Config 0.000GB
Local 0.000GB
Naveen 0.000GB

To list out all the collection use this command

>show collections;

Result: inside the naveen database

Books


Show element inside books collection:

>db.books.find()

Output:

{“_id” : ObjectId(“5cd….c1), “name” : “mongo book”}

Close command promt

Again open

Run mongod from anywhere show error internal and external command so need to set environment variable:


C:\users\naveen kumar\mongod 

press enter


Error External and Internal command

Set Environment Variable:

Choose path where django installed (C:\Program Files\MongoDB\Server\4.0\bin) and go to system start window choose system and click Advanced system setting. And then click on Environment Variable.


In system variable box find path double click on it.


Click on new and add the path

C:\Program Files\MongoDB\Server\4.0\bin


Now you can run anywhere by cmd like:

C:\users\naveen kumar\mongodb 

press enter



Import csv file into MangoDB


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 word.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:


<mongoimport -d wordCupDictionary -c dailywords --type CSV --file Contrib_data.csv --headerline

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


Import file(csv or json) in MongoDB Compass GUI :

First install MongoDB Compass from official website from tool option fill registration form by any userid, company name(any dumy) etc and install.


Create database and collection by click on create database button:



























Database created

Go to the collection on navigation bar and import csv file from here:























After Insert data it is look:




















Answer 1:INSTALL MONGODB


Import csv file:


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


> mongo

> show dbs

> use MongoDB

> show collections

>db.Contrib_data.findOne()


Answer 2:

> db.Contrib_data.find( { TRANSACTION_AMT: { $lt: 10 } } ).count()

84



Answer 3:

db.Contrib_data.update({},

{

$set: { "donar_cat" : ""

}

},false,true

)


db.Contrib_data.updateMany(

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

{

$set: { "donar_cat" : "big_fish"}

}

)


db.Contrib_data.updateMany(

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

{

$set: { "donar_cat" : "small fish"}

}

)


db.Contrib_data.aggregate(

[

{

$group:

{

_id : {donar_cat :"$donar_cat"},

totalAmount: { $sum: "$TRANSACTION_AMT"}

}

}

]

)


Answer 4:

db.Contrib_data.aggregate([

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

{ $limit : 5 },

{ $project : {_id: 0,cmte_received : 1,TRANSACTION_AMT: 1} }

]

)


Answer 5:

db.Contrib_data.aggregate([

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

{ $match : { "CAND_NAME" : "BACHMANN, MICHELE"} },

{ $project : {_id: 0,cmte_received : 1,CAND_NAME: 1,TRANSACTION_AMT:1} },

{ $limit : 5 }

]

)



Thanks! I hope it may help you to learn MongoDB Database Installation And Querying over database.


If you need any help related MongoDB or any other NoSQL Database Related Help then Contact Us or send your requirement details at:



And get instant help with an affordable price.



bottom of page