0% found this document useful (0 votes)
8 views46 pages

mongodb

The document provides a comprehensive guide on MongoDB, a NoSQL database, covering its configuration, CRUD operations, and data import/export functionalities. It details the steps to install MongoDB, set up necessary folders, and perform basic operations like inserting, querying, updating, and deleting documents. Additionally, it includes instructions for importing JSON and CSV files into MongoDB and exporting data from it.

Uploaded by

mushaf671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views46 pages

mongodb

The document provides a comprehensive guide on MongoDB, a NoSQL database, covering its configuration, CRUD operations, and data import/export functionalities. It details the steps to install MongoDB, set up necessary folders, and perform basic operations like inserting, querying, updating, and deleting documents. Additionally, it includes instructions for importing JSON and CSV files into MongoDB and exporting data from it.

Uploaded by

mushaf671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

MongoDB Documentation

By Nabeel Naqeebi
Table of Contents
What is MongoDB?................................................................................................................. 3
MongoDB Configuration ........................................................................................................10
MongoDB CRUD Operations .................................................................................................26
Insert Documents ..................................................................................................................27
insertOne...............................................................................................................................28
InsertMany ............................................................................................................................31
Query Documents .................................................................................................................32
findOne..................................................................................................................................33
findMany................................................................................................................................34
Update Documents ................................................................................................................34
updateOne ............................................................................................................................35
updateMany ..........................................................................................................................35
Delete Documents .................................................................................................................36
deleteOne..............................................................................................................................36
deleteMany ............................................................................................................................37
Import JSON file to MongoDB ...............................................................................................38
ASSIGNMENT.......................................................................................................................41
Exporting data from MongoDB ..............................................................................................42
Import CSV file into MongoDB ...............................................................................................44
Exporting data into CSV ........................................................................................................45
What is MongoDB?
MongoDB is a document-oriented NoSQL database used for high volume data storage.
Instead of using tables and rows as in the traditional relational databases, MongoDB makes
use of collections and documents. Documents consist of key-value pairs which are the basic
unit of data in MongoDB. Collections contain sets of documents and function which is the
equivalent of relational database tables. MongoDB is a database which came into light around
the mid-2000s.

Download link: https://www.mongodb.com/try/download/enterprise


Direct Link: https://downloads.mongodb.com/windows/mongodb-windows-x86_64-
enterprise-4.4.0-signed.msi
Download the file and then open it.
Press next

Agree the license agreement.


We are not done here. There are couple of steps we need to do before we can start using
MongoDB.

MongoDB Configuration
Step 1: Locate the folder where you have installed MongoDB. If you have followed the above
steps then you can find the folder at this location:

➢ C:\Program Files\MongoDB

Now create couple of folders that we need for MongoDB configuration.


1. Create two folders here, name them data and log.
2. Create another folder inside data and name it as db, that’s where all the data will be
stored.
That’s it, now close the window.

Step 2: Open command prompt (right click and run as administrator). Navigate to
the bin folder of MongoDB as shown in the screenshot. The path to the bin folder may be
different in your case based on where you have installed the MongoDB.
We can also run mongod server from anywhere else but for that we have to set the path.
Step 3: Configure the data & log folder and set MongoDB as service by typing this
command.

Note: This is a one line command.

mongod --directoryperdb --dbpath "C:\Program Files\MongoDB\data\db" --logpath "C:\Program


Files\MongoDB\log\mongo.log" --logappend –install
MongoDB server is running and now open another terminal and type “mongo”

For GUI part you can download “Roto 3T”


Download Link: https://robomongo.org/download
Open the downloaded “.zip” file and extract it. Now run “.exe” of “robo 3T”
It’s optional you can fill the required details and then click Finish or simply press Finish
without adding details.
Robo 3T is installed and now add the connection by clicking Monitor sign (or press Ctrl+N)
to add connection to the Server.

Enter the IP(as Address) and Name(as Connection name) in the required fields
Interface:

`
MongoDB CRUD Operations
CRUD operations are the basics operation in any DB. CRUD is acronym of Create Read
Update and Delete.
So, let’s start with Create clause. But before that let’s understand some terminologies of
MongoDB.

So, from the above table we come to know that we have to create a Collection (also known
as Table) in a Database.
First create a Database
Command: use Test_DB
Now create collection in Test_DB,(Database) for that use createCollection command
Command: db.createCollection(“myFirstCollection”)

Insert Documents
Now Insert the data into the collection, for that use insert Command
- Insert

One record is inserted with success. Here we use insert clause there are some other clauses
by which we can insert document into the respective collection. These are insert, insertOne
and insertMany. We have covered insert and now try the other ones.
insertOne

We have inserted one record using insertOne clause. What if we add more documents in
insertOne clause will it work or throw exception? Try it
Yes, it’s work.
But look we have only 3 records in the collection but we insert many records. What
happened, can you guess?
InsertMany

By using insertMany clause we can insert as many records as we can in a collection.

CREATING A COLLECTION

If the collection does not currently exist, insert operations will create the collection.
Query Documents
As inserted the data into a collection named “myFirstCollection”. In this part we will show the
data as output by using find command in mongoDB

We have 6 records in our collection. And by using find command we can list them all. It’s just
like doing select * from myCollection(in sql). But in sql we can also use the where clause
which only show the specific records. We can achieve the same result by adding condition
in find clause in MongoDB just the syntax is different.
It’s same as “ select * from myFirstCollection where customerId = ‘Nabeel’ ”. We can
also apply the other conditions on find clause i.e and, or, in etc.

findOne
By using findOne clause we can only view the first record of the collection.
findMany
In mongoDB to view all the documents in a collection we only use find{} clause, there is NO
findMany clause in mongoDB.

Update Documents
Update a document means that we want to change the previous inserted data. In MongoDB
we can update the single and multiple documents lets start with single document updation.
updateOne

It will update only one record (the first it finds)

updateMany

It will update all the documents which matches the condition.


We can also update only one record by using update command.

Delete Documents
Task: difference between remove vs delete

deleteOne
To delete a document from a collection we use delete command.
It will delete only one record.

deleteMany

By using deleteMany it will delete all the matching documents in the collection.
Import JSON file to MongoDB
Sample JSON:
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
}
}
}
}
Now copy this into a text editor and save it to any location in system.
Now we have to import into the MongoDB so the command will be as followed:

➢ mongoimport --db import_Export --collection import_json --file “E:\Big Data Live -


02\Course Material\sample_JSON.json”

So the JSON is imported into the import_json collection.

➢ Pretty() is used to display the output in json format


ASSIGNMENT
Exporting data from MongoDB
mongoexport --db import_Export --collection import_json -–out
E:\Mongo_Export_Example\exported_JSON.json –-pretty
Import CSV file into MongoDB
Sample CSV:
ProductName,Price,Quantity
1 Gallon Soy Milk,2.5,12
1 Gallon Almond Milk ,2.75,22
Six Pack Soda,3,3
Chocolate Bar,1,4
Paper Towels,1.25,8
Red Wine,14,3
Salt and Vinegar Chips ,2.25,22
Barbecue Chips,2.25,1
Gatorade,2,21
Breath Mints,0.5,11

mongoimport --type csv --db CSV_DB --collection import_CSV --headerline --drop “E:\Big
Data Live - 02\Course Material\ sample_CSV.csv”

Let’s validate the data


Exporting data into CSV
Command:
mongoexport --db CSV_DB --collection import_CSV --type=csv --fields
_id,ProductName,Price,Quantity --out "E:\Mongo_Export_Example/exported_CSV.csv"

You might also like