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

Lab01-MongoDB-BasicOperations

This document provides an introduction to NoSQL databases and MongoDB, highlighting their scalability, flexibility, and performance. It details the use of MongoDB Compass, a GUI tool for interacting with MongoDB, and outlines steps for installing MongoDB, creating a database, and performing basic operations such as inserting and querying documents. The lab experiment demonstrates practical applications of these concepts through hands-on activities with MongoDB Compass.

Uploaded by

bindushreekj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab01-MongoDB-BasicOperations

This document provides an introduction to NoSQL databases and MongoDB, highlighting their scalability, flexibility, and performance. It details the use of MongoDB Compass, a GUI tool for interacting with MongoDB, and outlines steps for installing MongoDB, creating a database, and performing basic operations such as inserting and querying documents. The lab experiment demonstrates practical applications of these concepts through hands-on activities with MongoDB Compass.

Uploaded by

bindushreekj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 1: Introduction to NoSQL and MongoDB using MongoDB Compass

Introduction to NoSQL and MongoDB

What is NoSQL?

NoSQL (Not Only SQL) databases provide a flexible and scalable way to store and retrieve
data. Unlike traditional relational databases (SQL-based), NoSQL databases do not enforce
a fixed schema, making them ideal for handling large volumes of unstructured or semi-
structured data.

Why NoSQL?

1. Scalability - Handles large amounts of data efficiently.


2. Flexibility - Schema-less design allows dynamic changes in data structure.
3. High Performance - Faster read and write operations.
4. Big Data & Real-Time Applications - Ideal for AI, ML, and IoT applications.

Introduction to MongoDB

MongoDB is a document-oriented NoSQL database that stores data in JSON-like BSON


(Binary JSON) format. It is widely used in modern applications due to its flexible schema,
high speed, and ease of scalability.

What is MongoDB Compass?

MongoDB Compass is a graphical user interface (GUI) tool provided by MongoDB to


interact with MongoDB databases visually, without using the command line. It allows
users to view, query, insert, update, and delete data in MongoDB collections easily.

Feature Description
Graphical UI Easy-to-use interface for working with MongoDB.
View Databases & Collections Lists all databases and collections available.
Insert, Update, Delete Documents Modify records without writing MongoDB commands.
Find Query Interface Allows filtering documents using JSON-based queries.
Aggregation Pipeline Helps perform complex data processing visually.
Schema Analysis Automatically detects and displays data structure.
Index & Performance Tools Monitors query execution time and indexing.
LAB EXPERIMENT-01

1a. Install MongoDB on your system (or use a pre-configured instance). Connect to the
database using the MongoDB shell. Verify your connection by listing all the databases.

1b. Create a new database named 'my_database'. Switch to this database and create a collection
named 'students'. Insert two sample documents into the 'students' collection, each representing
a student with the following fields: 'name', 'age', 'city'.

Aim

To understand the basics of NoSQL databases and MongoDB by installing MongoDB,


connecting to the database using MongoDB Compass, and performing basic operations such
as creating a database and inserting data.

1a. Install MongoDB and Connect to MongoDB Compass

Steps to Install MongoDB

For Windows:

1. Download MongoDB Community Edition from the official website:


https://www.mongodb.com/try/download/community
2. Install MongoDB, ensuring that the MongoDB Server and MongoDB Compass are
installed.
3. Start MongoDB as a Windows Service.
4. Open MongoDB Compass from the Start Menu.

Verify the Connection in MongoDB Compass

1. Open MongoDB Compass.


2. Click on "Connect" and select localhost: 27017 (default connection string).
3. Once connected, click "Databases" to view the list of existing databases.

Expected Output: A list of existing databases appears, confirming the connection is


successful.
1b. Create a Database and Insert Documents

Step 1: Create a New Database

1. Open MongoDB Compass.


2. Click on "Create Database".
3. Enter the Database Name as my_database.
4. Enter the Collection Name as students.
5. Click "Create Database".

Expected Output: A new database named my_database is successfully created, containing an


empty students collection.

Step 2: Insert Sample Documents into the 'students' Collection

1. Click on "my_database" and then click on "students".


2. Click "Insert Document".
3. Enter the following sample document representing a student:

{
"name": "Arjun",
"age": 22,
"city": "Bengaluru"
}

Click "Insert".

Insert another document:

{
"name": "Priya",
"age": 21,
"city": "Chennai"
}

Click "Insert".
Expected Output: Two documents are successfully inserted into the student’s collection. The
data should now be visible in MongoDB Compass.

4. Insert Multiple documents as an array.


[
{ "name": "Ravi", "age": 35, "city": "Chennai" },
{ "name": "Meera", "age": 29, "city": "Pune" },
{ "name": "Rakesh", "age": 21, "city": "Mysuru" },
{ "name": "Rahul", "age": 24, "city": "Hyderabad" },
{ "name": "Sneha", "age": 20, "city": "Mumbai" },
{ "name": "Karthik", "age": 25, "city": "Bengaluru" } ]

Step 3: Demonstrate find() Function Usage

1. Find All Documents

{}

Effect: Returns all records in the collection.

2️. Find by Name

{"name": "Arjun" }

Effect: Fetches the document where name is "Arjun".

3️. Find by City

{ "city": "Bengaluru" }

Effect: Fetches students from Bengaluru.

4️. Find Students Aged Above 21

{"age": {"$gt": 21 } }
Effect: Fetches all students with age > 21.

5️. Find Students Aged Between 20 and 25

{"age": {"$gte": 20, "$lte": 25 } }

Effect: Fetches students aged between 20 and 25.

6. Find Students from Bengaluru OR Chennai

$or Operator (Logical OR)

The $or operator ensures that at least one condition inside it must be true for a document to
match.It is used to find documents that satisfy either one condition or the other.

{ "$or": [ { condition1 }, { condition2 } ] }

{ "$or": [ { "city": "Bengaluru" }, { "city": "Chennai" } ] }

7.Find Students from Bengaluru AND Age > 21

$and Operator (Logical AND)

The $and operator ensures that all conditions inside it must be true for a document to match. It
is used to find documents that satisfy multiple conditions simultaneously.

{ "$and": [ { condition1 }, { condition2 } ] }

Example:

{ "$and": [ { "city": "Bengaluru" }, { "age": { "$gt": 21 } } ] }

9. Using $or Inside $and

Nesting in MongoDB refers to using one logical operator ($and, $or) inside another to create
more complex queries.

Find students who are from Bengaluru or Chennai AND have age > 21:

{
"$and": [

{ "$or": [ { "city": "Bengaluru" }, { "city": "Chennai" } ] },

{ "age": { "$gt": 21 } }

In this lab, we successfully installed MongoDB, connected to the database using MongoDB
Compass, created a new database and collection, and inserted sample documents. We also
verified the insertion by retrieving the stored data and demonstrated how to use the find()
function to query data efficiently.

You might also like