jagadish MongoDB_Practical_File[1]
jagadish MongoDB_Practical_File[1]
NOSQL+MONGODB
Program: BTECH (CSE Data Science and Artificial Intelligence (IBM))
4th Semester
Course Code: CSE405-22
Session: 2022-23
CT University, Ludhiana
Submitted in partial fulfilment of the requirement for the award of the Degree
of Bachelors of Data Science and Artificial Intelligence in Computer Science
Engineering along with IBM
1|Page
Table of Experiments
2|Page
EXPERIMENT-1
MongoDB Compass
Introduction
MongoDB Compass is a powerful GUI for querying, aggregating, and analysing
your MongoDB data in a visual environment. Compass is free to use and source
available, and can be run on macOS, Windows, and Linux.
3|Page
Query your data- Write ad-hoc queries or generate queries with
the help of AI to filter your data. Explore trends and
commonalities in your collections.
4|Page
Steps to install MongoDB Compass
Step-1: Open the browser and search “mongodb.com”.
Step-2: The webpage of MongoDB will appear.
5|Page
Step-4: After that, click on Installation.
After that, scroll down and you’ll see MongoDB Installation Tutorials. Here is
the list of all Installation Tutorials depending on your Operating System.
6|Page
Step-5: After selecting the Tutorial, scroll down a little bit and you will find the
Link “MongoDB Download Center”. Click the link and a new Window will open.
7|Page
A dropdown menu will appear.
8|Page
EXPERIMENT-2
MongoDB Shell
Introduction
MongoDB Shell is the quickest way to connect to (and work with) MongoDB.
Easily query data, configure settings, and execute other actions with this
modern, extensible command-line interface — replete with syntax highlighting,
intelligent autocomplete, contextual help, and error messages.
10 | P a g e
Steps to install VSCode
Step-1: Open the browser and search VSCode.
Step-2: Click the first link.
11 | P a g e
Step-4: Download the VSCode file according to your Software.
Step-5: After downloading, extract the file and install the VSCode.
Hence, VSCode will be installed.
12 | P a g e
EXPERIMENT-4
Setting MongoDB on VSCode
Step-1: Open VSCode.
Step-2: Click on Extension button and search for MongoDB extension, and then
install it. After installing, the MongoDB button will display at the bottom of the
button section.
13 | P a g e
Step-3: Click on MongoDB button and connect the Localhost, by clicking on it.
14 | P a g e
EXPERIMENT-5
Basic MongoDB Commands
show dbs: This command will display the available databases in MongoDB
shell.
show collections: This command will display all the collections in the database.
15 | P a g e
db.collection_name.drop(): This command will delete a collection.
db.collection_name.insertMany([{“Key:Value”},{“Key:Value”}]): This
command will insert multiple documents in the collection.
EXPERIMENT-6
Finding, Sorting and Limiting
db.collection_name.find(): This command will display all the documents
present in the collection.
16 | P a g e
db.collection_name.find().sort(“fieldname”): This command will display the
documents according to the type of fieldname.
If the field is of character datatype, then it will display the documents in
ascending alphabetical order.
17 | P a g e
If the field is of numeric datatype, then it will display the documents in
descending number order.
18 | P a g e
db.collection_name.find().sort(“fieldname”).limit(n): This command will
display the limited number of sorted documents.
EXPERIMENT-7
Query and Projection
db.collection_name.find(“{Query}”): This command will display all the
documents having the same query field with same value.
19 | P a g e
db.collection_name.find({},{Projection}): This command displays the specific
query fields from all the documents.
20 | P a g e
EXPERIMENT-8
Updating Documents
db.collection_name.updateOne({filter},{$set:{update}}): This command will
update the selected document field or add a new one by filter.
21 | P a g e
db.collection_name.updateMany({},{$set:{update}}): This command will
update all the selected document fields in all documents.
22 | P a g e
db.collection_name.updateMany({},{$unset:{fieldname}}): This command will
delete all the selected fields from all documents.
23 | P a g e
EXPERIMENT-9
Comparison Operators
Comparison Operators return data based on value operations.
$ne(Not Equal to): This operator will return those documents in which the
field value is not equal to the assigned one.
$gt(Greater than): This operator will return those documents in which the field
value is greater than the assigned one.
24 | P a g e
$gte(Greater Than or Equal to): This operator will return those documents in
which the field value is greater than or equal to the assigned one.
25 | P a g e
$lt(Less than): This operator will return those documents in which the field
value is less than the assigned one.
26 | P a g e
$lte(Less Than or Equal to): This operator will return those documents in
which the field value is not equal to the assigned one.
27 | P a g e
EXPERIMENT-10
Logical Operators
$and: Joins query clauses with a logical AND returns all documents that
matches the conditions of both clauses.
$or: Joins query clauses with a logical OR returns all documents that match the
conditions of either clause.
$nor: Joins query clauses with a logical NOR returns all documents that fail to
match both clauses.
28 | P a g e
$not: Inverts the effect of a query expression and returns documents that do
not match the query expression.
29 | P a g e
EXPERIMENT-11
Indexes
The index stores the value of a specific field or set of fields, ordered by the
value of the field. The ordering of the index entries supports efficient equality
matches and range-based query operations.
db.collection_name.find({Query}).explain(“executionStats”): This command
will display the execution stats of the query.
30 | P a g e
db.collection_name.createIndex(): This command will create an index.
31 | P a g e
EXPERIMENT-12
Aggregation
Aggregation is the process of selecting data from a collection to process
multiple documents and returns computed results.
$match: It filters the documents to pass only the documents that match the
specified condition.
$group: It groups and displays the selected key-value pairs, in which key is
assigned as _id, without repeating.
$count: It displays the total count of the documents having, the same key-
value pair.
EXPERIMENT-13
32 | P a g e
Deletion of Documents
db.collection_name.deleteOne({Query}): This command will delete a single
document.
33 | P a g e