MongoDB Flashcards

1
Q

Write a MongoDB query to match documents where the “field1” is equal to a specific value

A

db.collection.find({ field1: “specific_value” });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a MongoDB query to project only the “field1” and “field2” from a collection, excluding the “_id”.

A

db.collection.find({}, { field1: 1, field2: 1, _id: 0 });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a MongoDB query to compute a new field “total” by adding the values of “field1” and “field2”.

A

db.collection.aggregate([
{ $project: { total: { $add: [“$field1”, “$field2”] } } }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Use MongoDB to find documents where the size of the “arrayField” is greater than 5.

A

db.collection.find({ “arrayField”: { $size: { $gt: 5 } } });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a MongoDB query to conditionally select a “status” field based on the value of “quantity” (if greater than 10, set status to “High”, else “Low”).

A

db.collection.aggregate([
{
$project: {
status: {
$cond: { if: { $gt: [“$quantity”, 10] }, then: “High”, else: “Low” }
}
}
}
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use MongoDB to join documents from the “orders” collection with the “customers” collection based on the “customer_id” field.

A

db.orders.aggregate([
{
$lookup: {
from: “customers”,
localField: “customer_id”,
foreignField: “_id”,
as: “customerInfo”
}
}
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a MongoDB query to group documents by the “category” field and compute the total quantity for each category.

A

db.collection.aggregate([
{ $group: { _id: “$category”, totalQuantity: { $sum: “$quantity” } } }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Use MongoDB to unwind the “items” array from a parent document in the “orders” collection.

A

db.orders.aggregate([
{ $unwind: “$items” }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a MongoDB query to sort documents in the “students” collection based on the “name” field in ascending order.

A

db.students.find().sort({ “name”: 1 });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Use MongoDB to retrieve the first 10 documents from the “books” collection.

A

db.books.find().limit(10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a MongoDB query to find documents where “field1” is equal to “value1” and “field2” is less than 10.

A

db.collection.find({ field1: “value1”, field2: { $lt: 10 } });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a MongoDB query to project “quantity” and compute a new field “percentage” representing the percentage of “quantity” compared to a total value of 100.

A

db.collection.aggregate([
{ $project: { quantity: 1, percentage: { $multiply: [{ $divide: [“$quantity”, 100] }, 100] } } }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a MongoDB query to group documents by “category” and “subCategory,” computing the average price for each group, and then sorting the result by average price in descending order.

A

db.collection.aggregate([
{ $group: { _id: { category: “$category”, subCategory: “$subCategory” }, avgPrice: { $avg: “$price” } } },
{ $sort: { avgPrice: -1 } }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Use MongoDB to join documents from the “students” collection with the “courses” collection based on the “student_id” field and unwind the resulting array

A

db.students.aggregate([
{
$lookup: {
from: “courses”,
localField: “student_id”,
foreignField: “_id”,
as: “courseInfo”
}
},
{ $unwind: “$courseInfo” }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a MongoDB query to group documents by “status” and “type,” count the number of documents in each group, match only those groups with a count greater than 2, and limit the result to 5 documents.

A

db.collection.aggregate([
{ $group: { _id: { status: “$status”, type: “$type” }, count: { $sum: 1 } } },
{ $match: { count: { $gt: 2 } } },
{ $limit: 5 }
]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A