db.practice.find({ age: { $type: "int" } });
db.practice.find({ skills: { $size: 0 } });
// Find in array irrespective of position and element
db.practice.find({ skills: { $all: ["JavaScript", "GO"] } });
// Find in array of object even if object doesn't match exactly
db.practice.find({
skills: { $elemMatch: { name: "JAVASCRIPT", level: "Expert" } },
});
// To add element in an array | Don't replace the previous one | Don't add Duplicates
db.practice.updateMany({ }, { interests: { $addToSet: "Gardening" } } })
// For adding multiple elements
db.practice.updateMany({ }, { interests: { $addToSet: { $each: ["Gardening", "Sports"] } } } })
db.practice.updateMany({ }, { interests: { $pop: 1 } } }) // To remove the last element of an Array
db.practice.updateMany({ }, { interests: { $pull: "Gaming" } } }) // To remove a specific element
db.practice.updateMany({ }, { interests: { $pullAll: ["Gaming", "Gardening"] } } }) // To remove multiple specific elements
db.practice.updateMany({}, { $rename: { nmae: "name" } }); // To rename a field name
db.practice.updateMany({}, { $unset: { name: "" } }); // To remove a field
Example 1:
db.practice.aggregate([
// Stage 1: Match Document
{ $match: { gender: 'Female', age: { $gte: 18 } } },
// Stage 2: Add new collection with new field
{
$addFields: {
salary: {
$toInt: { $floor: { $multiply: [{ $rand: {} }, 100000] } }
}
}
},
// Stage 3: Reshape Data
{ $project: { name: 1, phone: 1, email: 1, occupation: 1, salary: 1, company: 1, address: 1 } },
// Stage 4: Create a new collection
{ $out: "user-with-salary" }
// State 5: Update Document (Note: Alternative of stage 4)
{$merge: "practice" }
])
Example 2:
db.practice.aggregate([
{ $group: { _id: "$occupation" } }, // Create _id with distinct value
]);
Example 3:
db.practice.aggregate([
{
$group: {
_id: null,
totalSalary: { $sum: "$salary" },
maxSalary: { $max: "$salary" },
minSalary: { $min: "$salary" },
averageSalary: { $avg: "$salary" },
},
},
{
$project: {
totalSalary: 1,
maxSalary: 1,
minSalary: 1,
averageSalary: { $toInt: { $round: ["$averageSalary"] } },
},
},
]);
Note: the $group
stage is primarily used for grouping documents and performing aggregate calculations, while the $project
stage is used for reshaping documents and selecting or creating fields.
Example 4:
db.practice.aggregate([
{ $unwind: "$friends" }, // For each friend it'll create another document and rest of the properties will be same
{ $group: { _id: "$friends", count: { $sum: 1 } } },
]);
Example 5: Multiple pipelines with $facet | $facet is used for sub-pipeline
db.practice.aggregate([
{ $match: { _id: ObjectId("6406ad65fc13ae5a400000c7") } },
{
$facet: {
skillsCount: [{ $project: { count: { $size: "$skills" } } }],
friendsCount: [{ $project: { count: { $size: "$friends" } } }],
},
},
]);
Example 5:
db.practice.aggregate([
{ $match: { gender: "Female", age: { $gte: 18 } } },
{
$lookup: {
from: "ShopCollections",
localField: "shop",
foreignField: "product",
as: "shop",
},
},
]);
Dummy Data Structure
[
{
name: "John Doe",
email: "[email protected]",
age: 28,
address: {
street: "123 Main St",
city: "New York",
state: "NY",
zipcode: "10001",
},
favorites: {
color: "blue",
food: "pizza",
movie: "The Shawshank Redemption",
},
friends: [
{
name: "Jane Smith",
email: "[email protected]",
},
{
name: "Mike Johnson",
email: "[email protected]",
},
],
},
];
Task 1: Find all users who are located in New York.
db.users.find({ "address.city": "New York" });
Task 2: Find the user(s) with the email "[email protected]" and retrieve their favorite movie.
db.users.find({ email: "[email protected]" }, { "favorites.movie": 1 });
Task 3: Find all users with "pizza" as their favorite food and sort them according to age.
db.users.find({ "favorites.food": "pizza" }).sort("age");
Task 4: Find all users over 30 whose favorite color is "green".
db.users.find({ age: { $gt: 30 }, "favorites.color": "green" });
Task 5: Count the number of users whose favorite movie is "The Shawshank Redemption."
db.users.find({ "favorites.movie": "The Shawshank Redemption" }).count();
Task 6: Update the zipcode of the user with the email "[email protected]" to "10002".
db.users.updateOne(
{ email: "[email protected]" },
{ $set: { "address.zipcode": "10002" } }
);
Task 7: Delete the user with the email "[email protected]" from the user data.
db.users.deleteOne({ email: "[email protected]" });
Task 8: Group users by their favorite movie and retrieve the average age in each movie group.
db.users.aggregate([
{
$group: { _id: "$favorites.movie", avgAge: { $avg: "$age" } },
},
]);
Task 9: Calculate the average age of users with the favorite " pizza " food.
db.users.aggregate([
{
$match: { "favorites.food": "pizza" },
},
{
$group: { _id: "$favorites.food", avgAge: { $avg: "$age" } },
},
]);
Task 1: Group users by their favorite color and retrieve the count of users in each color group.
db.users.aggregate([
{
$group: { _id: "$favorites.color", totalUser: { $sum: 1 } },
},
]);
Task 2: Find the user(s) with the highest age.
db.users.find({}).sort({ age: -1 }).limit(3);
Task 3: Find the most common favorite food among all users.
db.users.aggregate([
{
$group: { _id: "$favorites.food", usersLiked: { $sum: 1 } },
},
{
$sort: { usersLiked: -1 },
},
{
$limit: 1,
},
{
$project: { mostLikedFood: "$_id", _id: 0 },
},
]);
Task 4: Calculate the total number of friends across all users.
db.users.aggregate([
{
$group: {
_id: null,
totalFriends: { $sum: { $size: "$friends" } },
},
},
]);
Task 5: Find the user(s) with the longest name.
db.users.aggregate([
{
$project: {
name: 1,
age: 1,
address: 1,
nameLen: { $strLenCP: "$name" },
},
},
{
$sort: { nameLen: -1 },
},
{
$limit: 3,
},
]);
Task 6: Calculate each state's total number of users in the address field.
db.users.aggregate([
{ $group: { _id: "$address.state", totalUsers: { $sum: 1 } } },
]);
Task 7: Find the user(s) with the highest number of friends.
db.users.aggregate([
{
$project: {
name: 1,
email: 1,
totalFriends: { $size: "$friends" },
},
},
{
$sort: { totalFriends: -1 },
},
{
$limit: 1,
},
]);
Note: These tasks involve using various aggregation operators such as $group, $avg, $max, $sum, and $project to perform complex calculations and data transformations. You can write MongoDB aggregation queries to accomplish each task based on user data. Adjust the queries according to your specific implementation and requirements.
Dummy Data:
// orders
[
{
_id: 1,
order_number: "ORD-001",
customer_id: 1,
total_amount: 100.0,
},
{
_id: 2,
order_number: "ORD-002",
customer_id: 2,
total_amount: 150.0,
},
{
_id: 3,
order_number: "ORD-003",
customer_id: 1,
total_amount: 200.0,
},
]
// customers
[
({
_id: 1,
name: "Alice Williams",
email: "[email protected]",
},
{
_id: 2,
name: "Bob Anderson",
email: "[email protected]",
},
{
_id: 3,
name: "Emily Davis",
email: "[email protected]",
})
];
Bonus: Perform a lookup aggregation to retrieve the orders data along with the customer details for each order.
db.orders.aggregate([
{
$lookup: {
from: "customers",
localField: "customer_id",
foreignField: "_id",
as: "customerDetails",
},
},
]);