0% found this document useful (0 votes)
27 views

jagadish MongoDB_Practical_File[1]

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

jagadish MongoDB_Practical_File[1]

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

Practical File

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

SUBMITTED BY: SUBMITTED TO:


NAME: ABHINAV VERMA DR. DHRUV
REGISTRATION NO.: 72210986

1|Page
Table of Experiments

Name of the Experiments


S. No Page No
3-8
1 MongoDB Compass
9
2 MongoDB Shell
10-12
3 VSCode
13,14
4 Setting MongoDB on VSCode
15,16
5 Basic MongoDB commands
17-19
6 Finding, Sorting and Limiting
20,21
7 Query and Projection
22-24
8 Updating Documents
25-28
9 Comparison Operators
29,30
10 Logical Operators
31,32
11 Indexes
33
12 Aggregation
34
13 Deletion of Documents

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.

What more can be done with MongoDB Compass?


Visually Explore Your Data- Explore some of the tasks Compass can help you
accomplish, such as importing and managing data from an easy-to-navigate
interface.

1. Import your Data-


 Connect to your deployment- Connect to a MongoDB deployment
hosted on MongoDB Atlas, or a deployment hosted locally on your
own machine.
 Import your data- Import data from CSV or JSON files into your
MongoDB database.

2. Query your Data-


 Insert documents into your collections - Paste documents into the
JSON view, or manually insert documents using a field-by-field
editor.

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.

3. Create Aggregation pipelines-


 Insert documents into your collections - Insert documents into
your collections in two ways, JSON Mode and a Field-by-Field
Editor.
 Create aggregation pipelines- Write aggregation pipelines that
allow documents in a collection or view to pass through multiple
stages where they are processed into a set of aggregated results.

4. Run commands in the Shell-


 Connect to your deployment- Connect to a MongoDB deployment
hosted on MongoDB Atlas, or a deployment hosted locally on your
own machine.
 Work with your data in the MongoDB Shell - Use the embedded
MongoDB Shell in Compass to control your data in an interactive
JavaScript environment.

4|Page
Steps to install MongoDB Compass
Step-1: Open the browser and search “mongodb.com”.
Step-2: The webpage of MongoDB will appear.

Step-3: Go to resources and then Server Documentation.

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.

Choose any Installation Tutorial respective to 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.

Step-6: Scroll down, and click on “Select Package” button.

7|Page
A dropdown menu will appear.

Step-7: Change the msi package to zip.


Step-8: Click on Download.
Step-9: After downloading, extract the zip file and install it
Hence, the MongoDB compass is installed.

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.

Steps to install MongoDB Shell


Step-1: On the same downloading page, click on Mongo DB Shell.

Step-2: Click on Download.


Step-3: After downloading, go to the PC, then Downloads and unzip the folder.
Step-4: After extracting, cut the MongoDB folder and paste it in Program Files.
Step-5: After that follow this path:
C:\Program Files\MongoDB\Server\7.0\bin
Copy it and paste it in Environment Variables.
Give the New System Variable name: Mongosh, and in Variable Value, paste
the file path mentioned above.
Hence, the MongoDB shell is installed.
9|Page
EXPERIMENT-3
VS Code
Introduction
Visual Studio Code is one of the popular text editors used by professionals and
recommended to new coders. Being one of Microsoft’s renowned software, VS
code is free to use, open-source, and compatible with Windows, Linux, and
MacOS.

Why such popularity around Visual Studio Code?


VS Code can be installed easily and will not use too much space. Since this
platform is packed with robust and up-to-date features, its lightweight cannot
be considered a flaw.

By features, we mean VS code supports:


 A wide array of programming languages such as C++, Java, Python and
many more.
 It allows you to add essential extensions to the editor, such as debuggers
cloud and web development tools etc.

In addition to the above, their user-friendly interface is an added bonus. With


the editor neatly organized into discrete sections, programmers find it
convenient to write comprehensible codes and debug errors.

10 | P a g e
Steps to install VSCode
Step-1: Open the browser and search VSCode.
Step-2: Click the first link.

Step-3: Click on Download.

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.

Step-4: Open the terminal in VSCode and type “mongosh”.

Now, we can run MongoDB queries in VSCode.

14 | P a g e
EXPERIMENT-5
Basic MongoDB Commands
show dbs: This command will display the available databases in MongoDB
shell.

use ‘Database_name’: This command is either used to use another database


or to create a new Database.

db.dropDatabase(): This command is used to delete the entire database.

db.createCollection(‘collection_name’): This command is used to create a


collection in the database.

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.insertOne({“Key:Value”}): This command will insert a


single document in the 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.

db.collection_name.find().limit(n): this command will display the top n


documents, where n is any natural number.

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.

db.collection_name.findOne(“{Query}”): This command will display only one


document, which was inserted first by the user, according to the Query.

19 | P a g e
db.collection_name.find({},{Projection}): This command displays the specific
query fields from all the documents.

db.collection_name.find({Query},{Projection}): This command displays all the


projected fields from the documents having specific query fields.

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.

db.collection_name.updateOne({filter},{$unset:{fieldname}}): This command


will remove the selected document field by filter.

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.

db.collection_name.getIndexes(): This command will display all the indexes.

30 | P a g e
db.collection_name.createIndex(): This command will create an index.

db.collection_name.dropIndex(): This command will drop 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.

db.collection_name.deleteMany({Query}): This command will delete multiple


documents.

33 | P a g e

You might also like