0% found this document useful (0 votes)
17 views4 pages

MONGODB2

Uploaded by

yogesh bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

MONGODB2

Uploaded by

yogesh bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

{}

// $match Stage---------------------------

[Link]([
{
$match: { status: "A" }
}
])

[Link]([
{
$match: {
date: {
$gte: ISODate("2024-01-01"),
$lt: ISODate("2024-02-01")
}
}
}
])

[Link]([
{
$match: { quantity: { $gt: 2 } }
}
])

[Link]([
{
$match: { category: "Electronics" }
}
])

// -----------------------------------------------------
//group

[Link]([
{
$group: {
_id: "$product",
totalQuantitySold: { $sum: "$quantity" }
}
}
])

[Link]([
{
$group: {
_id: null,
totalAmountSpent: { $sum: "$amount" }
}
}
])
[Link]([
{
$match: { status: "B" },
$count: {"total statusof B:"}
}
])

[Link]([{
$group: {_id:null,
amount:{$avg:"$amount"}
}}]);

// ---------------------------------------------------sort

[Link]([
{
$sort: {amount:-1}
}
])

[Link]([
{
$group: {
_id: "$customerId",
totalSpending: { $sum: "$amount" }
}
},
{
$sort: { totalSpending: -1 }
}
]);

[Link]([
{
$sort:{"date": 1}
}])

[Link]([{
$sort:{amount:-1}
}])

// project ----------------

[Link]([
{
$project: {
product: 1,
amount: 1,
_id: 0
}
}
]);

[Link]([
{
$project: {
totalSpent: "$amount",
}
}
]);

[Link]([
{
$project:{
customerId:1,
customer:1
}
}])

// limit---------------------

[Link]([
{
$project: {
customerId:1,
_id: 0
}
},
{
$limit: 3
}
]);

// SKIP---------------------------

[Link]([
{
$project: {
product: 1,
amount: 1,
_id: 0
}
},
{
$skip: 1
}
]);

[Link]([
{
$lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customerDetails"
}
},
{
$unwind: "$customerDetails"
},
{
$match: {
"[Link]": { $regex: /^A/ }
}
}
])

// add fields

[Link]([
{
$addFields: {
totalPrice: { $multiply: ["$amount", "$quantity"] }
}
}
])

You might also like