MongoDB Flashcards
Write a MongoDB query to match documents where the “field1” is equal to a specific value
db.collection.find({ field1: “specific_value” });
Create a MongoDB query to project only the “field1” and “field2” from a collection, excluding the “_id”.
db.collection.find({}, { field1: 1, field2: 1, _id: 0 });
Write a MongoDB query to compute a new field “total” by adding the values of “field1” and “field2”.
db.collection.aggregate([
{ $project: { total: { $add: [“$field1”, “$field2”] } } }
]);
Use MongoDB to find documents where the size of the “arrayField” is greater than 5.
db.collection.find({ “arrayField”: { $size: { $gt: 5 } } });
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”).
db.collection.aggregate([
{
$project: {
status: {
$cond: { if: { $gt: [“$quantity”, 10] }, then: “High”, else: “Low” }
}
}
}
]);
Use MongoDB to join documents from the “orders” collection with the “customers” collection based on the “customer_id” field.
db.orders.aggregate([
{
$lookup: {
from: “customers”,
localField: “customer_id”,
foreignField: “_id”,
as: “customerInfo”
}
}
]);
Write a MongoDB query to group documents by the “category” field and compute the total quantity for each category.
db.collection.aggregate([
{ $group: { _id: “$category”, totalQuantity: { $sum: “$quantity” } } }
]);
Use MongoDB to unwind the “items” array from a parent document in the “orders” collection.
db.orders.aggregate([
{ $unwind: “$items” }
]);
Write a MongoDB query to sort documents in the “students” collection based on the “name” field in ascending order.
db.students.find().sort({ “name”: 1 });
Use MongoDB to retrieve the first 10 documents from the “books” collection.
db.books.find().limit(10);
Write a MongoDB query to find documents where “field1” is equal to “value1” and “field2” is less than 10.
db.collection.find({ field1: “value1”, field2: { $lt: 10 } });
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.
db.collection.aggregate([
{ $project: { quantity: 1, percentage: { $multiply: [{ $divide: [“$quantity”, 100] }, 100] } } }
]);
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.
db.collection.aggregate([
{ $group: { _id: { category: “$category”, subCategory: “$subCategory” }, avgPrice: { $avg: “$price” } } },
{ $sort: { avgPrice: -1 } }
]);
Use MongoDB to join documents from the “students” collection with the “courses” collection based on the “student_id” field and unwind the resulting array
db.students.aggregate([
{
$lookup: {
from: “courses”,
localField: “student_id”,
foreignField: “_id”,
as: “courseInfo”
}
},
{ $unwind: “$courseInfo” }
]);
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.
db.collection.aggregate([
{ $group: { _id: { status: “$status”, type: “$type” }, count: { $sum: 1 } } },
{ $match: { count: { $gt: 2 } } },
{ $limit: 5 }
]);