MongoDB Manual
MongoDB Manual
CSE-AI-DS
K.L.E. Society’s
LAB MANUAL
IV SEMESTER
MONGODB Lab
BDSL456B
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
Experiment No. 01
Aim:
a. Illustration of Where Clause, AND, OR operations in MongoDB.
b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query, Update,
Delete and Projection. (Note: use any collection)
Implementation:
01.a
db.createCollection("ProgrammingBooks")
db.ProgrammingBooks.insertMany([
{ title: "Clean Code", author: "Robert C. Martin", category: "Software Development", year: 2008 },
{ title: "JavaScript: The Good Parts", author: "Douglas Crockford", category: "JavaScript", year:
2008 },
{ title: "Design Patterns", author: "Erich Gamma", category: "Software Design", year: 1994 },
{ title: "Introduction to Algorithms", author: "Thomas H. Cormen", category: "Algorithms", year:
2009 },
{ title: "Python Crash Course", author: "Eric Matthes", category: "Python", year: 2015 }
]);
$or: [
{
$and: [
{ category: "Software Development" },
{ year: { $gt: 2007 } }
]
},
{ category: "Python" }
]
})
Implementation:
01.b
db.createCollection("Programming Books")
db. ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",
year: 2008
},
{
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},
{
title: "Introduction to Algorithms",
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
Experiment No. 02
Aim:
a. Develop a MongoDB query to select certain fields and ignore some fields of the documents
from any collection.
b. Develop a MongoDB query to display the first 5 documents from the results obtained in a.
[use of limit and find]
Implementation
2.a
db.createCollection("Movies")
db.Movies.insertMany([
{ title: "Inception", director: "Christopher Nolan", genre: "Science Fiction", year: 2010, ratings: {
imdb: 8.8, rottenTomatoes: 87 } },
{ title: "The Matrix", director: "Wachowskis", genre: "Science Fiction", year: 1999, ratings: { imdb:
8.7, rottenTomatoes: 87 } },
{ title: "The Godfather", director: "Francis Ford Coppola", genre: "Crime", year: 1972, ratings: {
imdb: 9.2, rottenTomatoes: 97 } }
]);
//To select only the title and director fields from the Movies collection
db.Movies.find({}, { title: 1, director: 1, _id: 0 })
Implementation
2.b
db.createCollection("Movies")
db.Movies.insertMany([
{ title: "Inception", director: "Christopher Nolan", genre: "Science Fiction", year: 2010, ratings: {
imdb: 8.8, rottenTomatoes: 87 } },
{ title: "The Matrix", director: "Wachowskis", genre: "Science Fiction", year: 1999, ratings: { imdb:
8.7, rottenTomatoes: 87 } },
{ title: "The Godfather", director: "Francis Ford Coppola", genre: "Crime", year: 1972, ratings: {
imdb: 9.2, rottenTomatoes: 97 } },
{ title: "Pulp Fiction", director: "Quentin Tarantino", genre: "Crime", year: 1994, ratings: { imdb:
8.9, rottenTomatoes: 92 } },
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
{ title: "The Shawshank Redemption", director: "Frank Darabont", genre: "Drama", year: 1994,
ratings: { imdb: 9.3, rottenTomatoes: 91 } },
{ title: "The Dark Knight", director: "Christopher Nolan", genre: "Action", year: 2008, ratings: {
imdb: 9.0, rottenTomatoes: 94 } },
{ title: "Fight Club", director: "David Fincher", genre: "Drama", year: 1999, ratings: { imdb: 8.8,
rottenTomatoes: 79 } }
]);
Experiment No. 03
Aim:
a. Execute query selectors (comparison selectors, logical selectors ) and list out the results on
any collection.
b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out the results on
any collection
Implementation
3a.
//Create the Employees Collection and Insert Documents
db.Employees.insertMany([
{ name: "Alice", age: 30, department: "HR", salary: 50000, joinDate: new Date("2015-01-15") },
{ name: "Bob", age: 24, department: "Engineering", salary: 70000, joinDate: new Date("2019-03-
10") },
{ name: "Charlie", age: 29, department: "Engineering", salary: 75000, joinDate: new Date("2017-
06-23") },
{ name: "David", age: 35, department: "Marketing", salary: 60000, joinDate: new Date("2014-11-
01") },
{ name: "Eve", age: 28, department: "Finance", salary: 80000, joinDate: new Date("2018-08-19") }
])
// $eq
db.Employees.find({ department: { $eq: "Engineering" } }).pretty()
//$ne
db.Employees.find({ department: { $ne: "HR" } }).pretty()
//$gt
db.Employees.find({ age: { $gt: 30 } }).pretty()
//$lt
db.Employees.find({ salary: { $lt: 70000 } }).pretty()
//$gte
db.Employees.find({ joinDate: { $gte: new Date("2018-01-01") } }).pretty()
//$lte
db.Employees.find({ age: { $lte: 28 } }).pretty()
$and: [
{ department: "Engineering" },
{ salary: { $gt: 70000 } }
]
}).pretty()
//$or
db.Employees.find({
$or: [
{ department: "HR" },
{ salary: { $lt: 60000 } }
]
}).pretty()
//$not
db.Employees.find({
department: {
$not: { $eq: "Engineering" }
}
}).pretty()
//$nor
db.Employees.find({
$nor: [
{ department: "HR" },
{ salary: { $gt: 75000 } }
]
})
Implementation
3b.
db.Places.insertMany([
{ name: "Central Park", location: { type: "Point", coordinates: [-73.9654, 40.7829] } },
{ name: "Times Square", location: { type: "Point", coordinates: [-73.9851, 40.7580] } },
{ name: "Brooklyn Bridge", location: { type: "Point", coordinates: [-73.9969, 40.7061] } },
{ name: "Empire State Building", location: { type: "Point", coordinates: [-73.9857, 40.7488] } },
{ name: "Statue of Liberty", location: { type: "Point", coordinates: [-74.0445, 40.6892] } }
])
//Find places near a specific coordinate, for example, near Times Square.
db.Places.find({
location: {
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
$near: {
$geometry: {
type: "Point",
coordinates: [-73.9851, 40.7580]
},
$maxDistance: 5000 // distance in meters
}
}
}).pretty()
// $geoWithin
db.Places.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-70.016, 35.715],
[-74.014, 40.717],
[-73.990, 40.730],
[-73.990, 40.715],
[-70.016, 35.715]
]
]
}
}
}
}).pretty()
//Bitwise Selectors
use techDB
db.Devices.insertMany([
{ name: "Device A", status: 5 }, // Binary: 0101
{ name: "Device B", status: 3 }, // Binary: 0011
{ name: "Device C", status: 12 }, // Binary: 1100
{ name: "Device D", status: 10 }, // Binary: 1010
{ name: "Device E", status: 7 } // Binary: 0111
])
//$bitsAllSet
//Find devices where the binary status has both the 1st and 3rd bits set
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
db.Devices.find({
status: { $bitsAllSet: [0, 2] }
}).pretty()
//$bitsAnySet
//Find devices where the binary status has at least the 2nd bit set
db.Devices.find({
status: { $bitsAnySet: [1] }
}).pretty()
//$bitsAllClear
//Find devices where the binary status has both the 2nd and 4th bits clear
db.Devices.find({
status: { $bitsAllClear: [1, 3] }
}).pretty()
//$bitsAnyClear
//Find devices where the binary status has at least the 1st bit clear
db.Devices.find({
status: { $bitsAnyClear: [0] }
})
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
Experiment No. 04
Aim:
Create and demonstrate how projection operators ($, $elematch and $slice) would be used in the
MongoDB.
Implementation
db.Products.insertMany([
{
name: "Laptop",
brand: "BrandA",
features: [
{ name: "Processor", value: "Intel i7" },
{ name: "RAM", value: "16GB" },
{ name: "Storage", value: "512GB SSD" }
],
reviews: [
{ user: "Alice", rating: 5, comment: "Excellent!" },
{ user: "Bob", rating: 4, comment: "Very good" },
{ user: "Charlie", rating: 3, comment: "Average" }
]
},
{
name: "Smartphone",
brand: "BrandB",
features: [
{ name: "Processor", value: "Snapdragon 888" },
{ name: "RAM", value: "8GB" },
{ name: "Storage", value: "256GB" }
],
reviews: [
{ user: "Dave", rating: 4, comment: "Good phone" },
{ user: "Eve", rating: 2, comment: "Not satisfied" }
]
}
])
Experiment No. 05
Aim:
Execute Aggregation operations ($avg, $min, $max, $push, $addToSet etc.). students encourage to
execute several queries to demonstrate various aggregation operators)
db.Sales.insertMany([
{ date: new Date("2024-01-01"), product: "Laptop", price: 1200, quantity: 1, customer: "Amar" },
{ date: new Date("2024-01-02"), product: "Laptop", price: 1200, quantity: 2, customer: "Babu" },
{ date: new Date("2024-01-03"), product: "Mouse", price: 25, quantity: 5, customer: "Chandra" },
{ date: new Date("2024-01-04"), product: "Keyboard", price: 45, quantity: 3, customer: "Amar" },
{ date: new Date("2024-01-05"), product: "Monitor", price: 300, quantity: 1, customer: "Babu" },
{ date: new Date("2024-01-06"), product: "Laptop", price: 1200, quantity: 1, customer: "Deva" }
])
db.Sales.aggregate([
{
$group: {
_id: "$product",
averagePrice: { $avg: "$price" }
}
}
]).pretty()
db.Sales.aggregate([
{
$group: {
_id: "$product",
minPrice: { $min: "$price" }
}
}
]).pretty()
db.Sales.aggregate([
{
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
$group: {
_id: "$product",
maxPrice: { $max: "$price" }
}
}
]).pretty()
//Group sales by customer and push each purchased product into an array.
db.Sales.aggregate([
{
$group: {
_id: "$customer",
products: { $push: "$product" }
}
}
]).pretty()
//Group sales by customer and add each unique purchased product to an array.
db.Sales.aggregate([
{
$group: {
_id: "$customer",
uniqueProducts: { $addToSet: "$product" }
}
}
]).pretty()
db.Sales.aggregate([
{
$group: {
_id: "$product",
totalQuantity: { $sum: "$quantity" },
totalSales: { $sum: { $multiply: ["$price", "$quantity"] } },
customers: { $addToSet: "$customer" }
}
}
])
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
Experiment No. 06
Aim:
Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group, $sort, $project,
$skip etc. students encourage to execute several queries to demonstrate various aggregation operators).
{
name: "Taco Stand",
cuisine: "Mexican",
location: "Jayanagar",
reviews: [
{ user: "Ishaan", rating: 5, comment: "Fantastic tacos!" },
{ user: "Jaya", rating: 4, comment: "Very authentic" }
]
}
])
Experiment No. 07
Aim:
a. Find all listings with listing_url, name, address, host_picture_url in the listings And Reviews
collection that have a host with a picture url
b. Using E-commerce collection write a query to display reviews summary.
Implementation
7a.
// Switch to the vacationRentals database
use vacationRentals
country: "Wonderland"
},
host: {
name: "Charlie",
picture_url: "http://www.example.com/images/host/host789.jpg"
}
}
])
Implementation
7b.
// Switch to the ecommerce database
use ecommerce
Experiment No. 08
Aim:
a. Demonstrate creation of different types of indexes on collection (unique, sparse, compound and
multi key indexes),
b. Demonstrate optimization of queries using indexes.
Implementation
8a.
Experiment No. 09
Aim:
Develop a query to demonstrate Text search using catalog data collection for a given word b.
Develop queries to illustrate excluding documents with certain words and phrases
BDSL456B MONGODB Lab IV Sem. CSE-AI-DS
Experiment No. 10
Aim: