MongoDB - Lab 11
MongoDB - Lab 11
Contents:
1. Overview of MongoDB
2. Difference in terminology of MongoDB
3. Installation of MongoDB
4. Designing Schema in MongoDB
5. Some important methods in MongoDB
6. Creating Database
7. Creating collections
8. Inserting single/multiple Documents
9. Querying, Deleting & updating of Documents
10. Logical Operations
11. Implementation of where clause
Overview of MongoDB
MongoDB is an open-source document database and leading NoSQL database. MongoDB
is written in C++. This manual will give you great understanding on MongoDB concepts
needed to create and deploy a highly scalable and performance-oriented database.
Database
Database is a physical container for collections. Each database gets its own set of files on
the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A
collection exists within a single database. Collections do not enforce a schema. Documents
within a collection can have different fields. Typically, all documents in a collection are of
similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema
means that documents in the same collection do not need to have the same set of fields or
structure, and common fields in a collection's documents may hold different types of data.
3. Now further move to the sub sub directory of the mongodb i.e. bin directory
4. Now run dir command on bin directory i.e. sub sub directory of mongoDB and match
the number files as shown in the given screenshot
5. Now run mongod command. There is an error in the given screenshot while running
mongod command so in order to handle this problem first we need to run mkdir
\data\db then mongod command
CL 2005 – Database Systems Lab Lab Manual - 11
7. Now again run mongod command then connection is now waiting to connect as
shown in the screenshot
8. Now we are establishing the mongod and mongo connection in the given screenshot
CL 2005 – Database Systems Lab Lab Manual - 11
Example
Suppose a client needs a database design for his blog/website and see the differences
between RDBMS and MongoDB schema design. Website has the following requirements.
1. Every post has the unique title, description and url.
2. Every post can have one or more tags.
3. Every post has the name of its publisher and total number of likes.
4. Every post has comments given by users along with their name, message, data-time and
likes.
5. On each post, there can be zero or more comments
In RDBMS schema, design for above requirements will have minimum three tables.
While in MongoDB schema, design will have one collection post and the following structure:
CL 2005 – Database Systems Lab Lab Manual - 11
So while showing the data, in RDBMS you need to join three tables and in MongoDB, data
will be shown from one collection only.
Syntax
Basic syntax of use DATABASE statement is as follows − use DATABASE_NAME
Command:
>show dbs
If you want to check your databases list, use the command show dbs.
Your created database (myfirstdb) is not present in list. To display database, you need to
insert at least one document into it.
Syntax
Basic syntax of dropDatabase() command is as follows −
> db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will
delete default 'test' database.
Example
If you want to delete new database <mydb>, then dropDatabase() command would be as
follows −
>use myfirstdb
// Switched to db mydb
>db.dropDatabase()
{ "dropped" : "myfirstdb", "ok" : 1 }
Syntax
Basic syntax of createCollection() command is as follows –
db.createCollection(name, options)
Syntax
Basic syntax of drop() command is as follows −
db.COLLECTION_NAME.drop()
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insert(document)
Example
> db.mycollection.insert({
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Amin Sadiq',
url: 'http://www.gmail.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
CL 2005 – Database Systems Lab Lab Manual - 11
Notepad: Here mycollection is our collection name, as created in the previous slide. If the
collection doesn't exist in the database, then MongoDB will create this collection and then
insert a document into it.
_id parameter
In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a
unique ObjectId for this document.
db.mycollection.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Amin Sadiq',
url: 'http://www.gmail.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'Ali Shah Fatmi',
url: 'http://www.gmail.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2021,11,10,2,35),
like: 0
}
]
}
])
CL 2005 – Database Systems Lab Lab Manual - 11
Syntax
• The basic syntax of find() method is as follows −
• >db.COLLECTION_NAME.find()
Note: find() method will display all the documents in a non-structured way.
Syntax
• >db.mycol.find().pretty()
CL 2005 – Database Systems Lab Lab Manual - 11
AND in MongoDB
Syntax
In the find() method, if you pass multiple keys by separating them by ',' then MongoDB treats
it as AND condition. Following is the basic syntax of AND –
db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by ‘Amin Sadiq' and whose title is
'MongoDB Overview'.
CL 2005 – Database Systems Lab Lab Manual - 11
OR in MongoDB
Syntax
• To query documents based on the OR condition, you need to use $or keyword. Following
is the basic syntax of OR –
db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by ‘Amin Sadiq' or whose title is 'MongoDB
Overview'.
Syntax
• The basic syntax of update() method is as follows −
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example
Consider the mycollection collection has the following data.
Following example will set the new title 'New MongoDB Tutorial' of the documents whose title
is 'MongoDB Overview'.
Syntax
Basic syntax of remove() method is as follows –
db.COLLECTION_NAME.remove(DELETION_CRITERIA)
Example
Let’s consider the mycollection collection has the following data.
Following example will remove all the documents whose title is 'MongoDB Overview'.