Mongodb_Question Bank for IA
Mongodb_Question Bank for IA
1b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query,
Update, Delete, and Projection:
1. Write a query to retrieve documents from a collection but return only specific fields
(e.g., name and age) and ignore others (e.g., address).
2b. Develop a MongoDB query to display the first 5 documents from the results
obtained in 2a:
1. Modify the query from 2a to limit the results to the first 5 documents.
3a. Execute query selectors (comparison selectors, logical selectors) and list out the
results on any collection:
1. Demonstrate the use of comparison selectors (e.g., $gt, $lt, $eq) in a MongoDB
query.
2. Write a query using logical selectors (e.g., $and, $or, $not) and display the results.
"_id": 1, "_id": 2,
"product_name": "Laptop", "product_name": "Smartphone",
"price": 1200, "price": 800,
"stock_quantity": 15, "stock_quantity": 20,
"category": "Electronics" "category": "Electronics"
"_id": 3, "_id": 4,
"product_name": "Chair", "product_name": "Table",
"price": 100, "price": 250,
"stock_quantity": 30, "stock_quantity": 10,
"category": "Furniture" "category": "Furniture"
4. Create and demonstrate how projection operators ($, $elemmatch, and $slice) would
be used in MongoDB:
1. Demonstrate the use of the $ operator in projections to return specific elements from
an array field.
2. Use the $elemMatch projection operator to return documents where an array field
matches a specified condition.
3. Show how to use the $slice operator to limit the number of array elements returned.
1. Write an aggregation query using the $avg operator to calculate the average value of a
field.
2. Develop an aggregation query using the $min and $max operators to find the
minimum and maximum values of a field.
3. Demonstrate the use of the $push operator in an aggregation pipeline to append
values to an array.
4. Use the $addToSet operator in an aggregation pipeline to add values to an array while
ensuring uniqueness.
"_id": 1, "_id": 2,
"product": "A", "product": "B",
"quantity": 10, "quantity": 5,
"price": 15 "price": 20.
"_id": 3, "_id": 4,
"product": "C", "product": "D",
"quantity": 8, "quantity": 12,
"price": 25 "price": 18
"_id": 5, "_id": 6,
"product": "E", "product": "F",
"quantity": 6, "quantity": 10,
"price": 22 "price": 30
6) 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": "Alice", "age": 24, "grade": "A", "score": 85, "subject": "Math" },
{ "name": "Bob", "age": 22, "grade": "B", "score": 78, "subject": "Math" },
{ "name": "Charlie", "age": 23, "grade": "A", "score": 92, "subject": "Science" },
{ "name": "David", "age": 24, "grade": "C", "score": 65, "subject": "Math" },
{ "name": "Eve", "age": 22, "grade": "B", "score": 80, "subject": "Science" },
{ "name": "Frank", "age": 23, "grade": "A", "score": 88, "subject": "Math" },
{ "name": "Grace", "age": 24, "grade": "C", "score": 70, "subject": "Science" }
1. $match: Filter students who are 21 years or older. $group: Group by grade and
calculate the average age. $sort: Sort by average age in descending order. $project:
Project the grade and average age. $skip: Skip the first result.
7. 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.
"listing_url": "https://example.com/listing/1",
"name": "Charming Apartment",
"address": "123 Main St, Anytown, USA",
"host": "host_picture_url": https://example.com/host/1/picture.jpg
Write a query to find all listings where the host does not have a picture URL. Include the fields
listing_url, name, address, and host.host_picture_url in the output.
Write a query to display the total number of reviews and the average rating for each
product, but only include products that have more than 2 reviews.
8 Demonstrate creation of different types of indexes on collection (unique, sparse,
compound and multikey indexes)
"product_id": 3, "product_id": 2, "product_name": "product_id": 1,
"product_name": "USB-C "Bluetooth Keyboard", "price": "product_name": "Wireless
Hub", 45.99, "category": "Electronics", Mouse",
"price": 29.99, "tags": ["keyboard", "bluetooth", "price": 25.99,
"category": "Accessories", "electronics"] "category": "Electronics",
"tags": ["usb-c", "hub", "tags": ["mouse", "wireless",
"accessories"] "electronics"]
Write a query to create a unique index on the email field of a users collection to ensure no two
users have the same email address
Create a compound index on the firstName and lastName fields in a contacts collection to
optimize searches by both fields.
Create a multikey index on the skills field in a resumes collection, where skills is an array of
strings.
9 a. Develop a query to demonstrate Text search using catalog data collection for a
given word . Develop queries to illustrate excluding documents with certain words and
phrases.
"product_id": 1, "product_id": 2, "product_id": 3, "
"title": "Wireless Mouse", "title": "Bluetooth Keyboard", title": "USB-C Hub",
"description": "A high-quality "description": "An ergonomic "description": "A versatile USB-C
wireless mouse with ergonomic keyboard with Bluetooth hub for multiple devices."
design." connectivity."
"product_id": 4, "product_id": 5,
"title": "Gaming Laptop", "title": "Wireless Earbuds",
"description": "A high- "description": "Noise-cancelling
performance laptop designed for wireless earbuds with long battery
gaming." life."
Write a query to find documents that contain "wireless" but exclude those that contain "mouse".
Write a query to exclude documents that contain either "gaming" or "laptop".
Write a query to find documents that contain both "wireless" and "earbuds"
10. Develop an aggregation pipeline to illustrate Text search on Catalog data collection.
Evaluation Rubrics
1) Copy down the question
2) Procedure to execute the steps in MongoDB
3) Syntax for the query
4) Expected Outcomes