Experiment 4
Data Set
db.students.insertMany([
{ name: "Alice", grades: [{ subject: "Math", score: 85 }, { subject: "Science", score: 90 }] },
{ name: "Bob", grades: [{ subject: "Math", score: 78 }, { subject: "Science", score: 88 }] }
])
4.(b) $elemMatch Projection Operator
db.students.find({}, { grades: { $elemMatch: { score: { $gt: 80 } } } })
Explanation:
• $elemMatch returns only the array elements that match the condition
(score > 80).
4(c) $slice Projection Operator
db.students.find({}, { grades: { $slice: 1 } })
Explanation:
• $slice: 1 returns only the first element of the grades array.
Experiment 5: Aggregation Operations
Aggregation operations allow us to perform computations on data.
Dataset
db.people.insertMany([
{ name: "John", age: 25 },
{ name: "Alice", age: 30 },
{ name: "Bob", age: 22 },
{ name: "Mark", age: 28 }
])
(a) $avg (Average Age)
db.people.aggregate([{ $group: { _id: null, avgAge: { $avg: "$age" } } }])
(b) $min (Minimum Age)
db.people.aggregate([{ $group: { _id: null, minAge: { $min: "$age" } } }])
(c) $max (Maximum Age)
db.people.aggregate([{ $group: { _id: null, maxAge: { $max: "$age" } } }])
(d) $push (Collect All Names)
db.people.aggregate([{ $group: { _id: null, allNames: { $push: "$name" } } }])
(e) $addToSet (Unique Names)
db.people.aggregate([{ $group: { _id: null, uniqueNames: { $addToSet:
"$name" } } }])
Experiment 6: Aggregation Pipeline
The Aggregation Pipeline in MongoDB is a powerful framework for data
processing. It processes documents in stages, transforming them step by step
Dataset
db.users.insertMany([
{ name: "John", age: 35, gender: "Male" },
{ name: "Alice", age: 40, gender: "Female" },
{ name: "Bob", age: 45, gender: "Male" },
{ name: "Jane", age: 38, gender: "Female" }
])
a) Pipelining
db.users.aggregate([
{ $match: { age: { $gt: 38 } } }, // Step 1: Filter users older than 38
{ $group: { _id: "$gender", count: { $sum: 1 } } }, // Step 2: Group by gender, count
users
{ $sort: { count: -1 } }, // Step 3: Sort by count (descending)
{ $skip: 2 }, // Step 4: Skip the first 2 results
{ $project: { _id: 0, gender: "$_id", count: 1 } } ]) // Step 5: Format the output
Experiment 7: Finding Listings & Reviews
Dataset
db.listingsAndReviews.insertMany([
{ listing_url: "url1", name: "Hotel A", address: "City X", host_picture_url:
"img1.jpg" },
{ listing_url: "url2", name: "Hotel B", address: "City Y", host_picture_url:
"img2.jpg" }
])
Find Listings
db.listingsAndReviews.find({}, { listing_url: 1, name: 1, address: 1,
host_picture_url: 1 })
Experiment 8:
a. Demonstrate creation of different types of indexes on collection:
Dataset
db.collection.insertMany([
{ _id: 1, username: "john_doe", age: 28, city: "New York", tags: ["sports",
"tech"] },
{ _id: 2, username: "alice_smith", age: 32, city: "Los Angeles", tags: ["fashion",
"travel"] },
{ _id: 3, username: "bob_jackson", age: 40, city: "Chicago", tags: ["food",
"fitness"] },
{ _id: 4, username: "charlie_adams", age: 22, city: "San Francisco", tags:
["gaming", "music"] },
{ _id: 5, username: "david_lee", age: 35, city: null, tags: ["photography"] },
{ _id: 6, username: "emma_watson", age: 27, city: "Seattle", tags: ["tech",
"education"] },
]);
Unique Index
Ensures that each username in the collection is unique.
db.collection.createIndex({ username: 1 }, { unique: true })
Sparse Index
Creates an index on city, but only for documents where city exists (ignores
null or missing fields).
db.collection.createIndex({ city: 1 }, { sparse: true })
Compound Index
Creates an index on age (ascending) and city (descending).
db.collection.createIndex({ age: 1, city: -1 })
Multikey Index
Indexes arrays stored in the tags field.
db.collection.find({ tags: "tech" })
Experiment 9: Text Search
Dataset
db.catalog.insertMany([
{ productName: "Laptop", description: "High performance laptop" },
{ productName: "Phone", description: "Smartphone with AI features" }
])
Search for a Word
db.catalog.find({ $text: { $search: "laptop" } })
Summary
Experiment Operation Purpose
Control which fields/array elements are
4 Projection Operators
returned.
Perform calculations (avg, min, max,
5 Aggregation Operators
push, addToSet).
Filters, groups, and processes documents
6 Aggregation Pipeline
in stages.
Retrieve only certain fields from
7 Find Specific Fields
documents.
8 Indexes Optimize queries with indexes.
Experiment Operation Purpose
9 Text Search Search for words within text fields.
Aggregation for Text Use aggregation to filter and sort text
10
Search search results.