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

Mongodb_Question Bank for IA

The document serves as a comprehensive MongoDB question bank covering various operations such as filtering documents using where clauses, executing commands for insert, query, update, delete, and projection, as well as aggregation operations. It includes practical examples and queries for using comparison and logical selectors, projection operators, and creating different types of indexes. Additionally, it provides guidelines for text search and aggregation pipelines, along with evaluation rubrics for executing MongoDB queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Mongodb_Question Bank for IA

The document serves as a comprehensive MongoDB question bank covering various operations such as filtering documents using where clauses, executing commands for insert, query, update, delete, and projection, as well as aggregation operations. It includes practical examples and queries for using comparison and logical selectors, projection operators, and creating different types of indexes. Additionally, it provides guidelines for text search and aggregation pipelines, along with evaluation rubrics for executing MongoDB queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MongoDB Question Bank

1a. Illustration of Where Clause, AND, OR operations in MongoDB:

1. Illustrate how to use where clause in MongoDB to filter documents based on a


condition.
2. Explain how to combine multiple conditions using the AND operator in MongoDB
queries.
3. Demonstrate the use of the OR operator to filter documents that match any of the given
conditions.

"_id": 1, "_id": 2, "_id": 3,


"name": "John Doe", "name": "Jane Smith", "name": "Mike Johnson",
"age": 28, "age": 34, "age": 45,
"department": "Sales", "department": "Marketing", "department": "HR",
"salary": 50000 "salary": 60000 "salary": 70000
"_id": 4, "_id": 5, "_id": 6,
"name": "Emily Davis", "name": "Robert Brown", "name": "Lucy White",
"age": 22, "age": 31, "age": 29,
"department": "department": "Sales", "department": "Marketing",
"Development", "salary": 52000 "salary": 62000
"salary": 55000

1b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query,
Update, Delete, and Projection:

1. Write a MongoDB command to insert multiple documents into a collection.


2. Develop a query to retrieve all documents from a collection.
3. Write an update command to modify existing documents based on a condition.
4. Demonstrate how to delete documents from a collection using a specific condition.
5. Use the projection feature in a query to return only certain fields of the documents.

"_id": 1, "_id": 2, "_id": 3,


"name": "John Doe", "name": "Jane Smith", "name": "Mike Johnson",
"age": 28, "age": 34, "age": 45,
"department": "Sales", "department": "Marketing", "department": "HR",
"salary": 50000 "salary": 60000 "salary": 70000
"_id": 4, "_id": 5, "_id": 6,
"name": "Emily Davis", "name": "Robert Brown", "name": "Lucy White",
"age": 22, "age": 31, "age": 29,
"department": "department": "Sales", "department": "Marketing",
"Development", "salary": 52000 "salary": 62000
"salary": 55000
2a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents from any collection:

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).

"_id": 1, "_id": 2, "_id": 3,


"name": "Alice", "name": "Bob", "name": "Charlie",
"age": 25, "age": 28, "age": 23,
"address": "123 Maple "address": "456 Oak "address": "789 Pine
Street", " Avenue", Road",
gpa": 3.8, "gpa": 3.5, "gpa": 3.9,
"courses": ["Math", "courses": ["Biology", "courses": ["Art",
"Physics", "Computer "Chemistry", "Statistics"] "Literature", "History"]
Science"]

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.

"_id": 1, "_id": 2, "_id": 3,


"name": "Alice", "name": "Bob", "name": "Charlie",
"age": 25, "age": 28, "age": 23,
"address": "123 Maple "address": "456 Oak "address": "789 Pine
Street", " Avenue", Road",
gpa": 3.8, "gpa": 3.5, "gpa": 3.9,
"courses": ["Math", "courses": ["Biology", "courses": ["Art",
"Physics", "Computer "Chemistry", "Statistics"] "Literature", "History"]
Science"]

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.

"_id": 1, "_id": 2, "_id": 3,


"title": "Book 1", "title": "Book 2", "title": "Book 3",
"authors": [ "authors": [ "authors": [ "name":
"name": "Author A", "name": "Author A", "Author B", "age": 40 ,
"age": 35 , "age": 35 , "name": "Author E",
"name": "Author B", "name": "Author D", "age": 38 ,
"age": 40, "age": 45. "name": "Author F",
"name": "Author C", "age": 36 .
"age": 30.

5. Execute Aggregation operations ($avg, $min, $max, $push, $addToSet etc.).


Encourage students to execute several queries to demonstrate various aggregation
operators:

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" }

Write a query to find all students who have a

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.

7 b. Using E-commerce collection write a query to display reviews summary.


"product_id": 1, "product_id": 2, "product_id": 3,
"product_name": "Wireless "product_name": "Bluetooth "product_name": "USB-C
Mouse", Keyboard", Hub",
"reviews":{ "review_id": 101, "reviews": [ { "review_id": 103, "reviews": { "review_id":
"rating": 4, "comment": "Good "rating": 3, "comment": "Average", 105, "rating": 5, "comment": "Very
quality", "user": "Alice" "user": "Charlie" }, useful", "user": "Eve" }
{ "review_id": 102, "rating": 5, { "review_id": 104,
"comment": "Excellent!", "user": "rating": 4, "comment": "Works
"Bob" } well", "user": "Dave" }

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.

"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 perform this aggregation pipeline to search for the word "wireless" and
perform various operations such as projecting specific fields, sorting, and limiting the results.

Evaluation Rubrics
1) Copy down the question
2) Procedure to execute the steps in MongoDB
3) Syntax for the query
4) Expected Outcomes

You might also like