mongodb
mongodb
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.
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
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.
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
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
updateMany
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 --type csv --db CSV_DB --collection import_CSV --headerline --drop “E:\Big
Data Live - 02\Course Material\ sample_CSV.csv”