आप $cond . का उपयोग कर सकते हैं जांच करने के लिए ऑपरेटर:
$monthहै<= 3,quarter. नाम की फ़ील्ड प्रोजेक्ट करें "एक" के रूप में मूल्य के साथ।$monthहै<= 6,quarter. नाम की फ़ील्ड प्रोजेक्ट करें "दो" के रूप में मूल्य के साथ।$monthहै<= 9,quarter. नाम की फ़ील्ड प्रोजेक्ट करें "तीन" के रूप में मूल्य के साथ।- अन्य फ़ील्ड का मान
quarter"चौथा" होगा। - फिर
$groupquarter. द्वारा फ़ील्ड.
कोड:
db.collection.aggregate([
{
$project: {
date: 1,
quarter: {
$cond: [
{ $lte: [{ $month: "$date" }, 3] },
"first",
{
$cond: [
{ $lte: [{ $month: "$date" }, 6] },
"second",
{
$cond: [{ $lte: [{ $month: "$date" }, 9] }, "third", "fourth"],
},
],
},
],
},
},
},
{ $group: { _id: { quarter: "$quarter" }, results: { $push: "$date" } } },
]);
आपके स्कीमा के लिए विशिष्ट:
db.collection.aggregate([
{
$project: {
dateAttempted: 1,
userId: 1,
topicId: 1,
ekgId: 1,
title: 1,
quarter: {
$cond: [
{ $lte: [{ $month: "$dateAttempted" }, 3] },
"first",
{
$cond: [
{ $lte: [{ $month: "$dateAttempted" }, 6] },
"second",
{
$cond: [
{ $lte: [{ $month: "$dateAttempted" }, 9] },
"third",
"fourth",
],
},
],
},
],
},
},
},
{ $group: { _id: { quarter: "$quarter" }, results: { $push: "$$ROOT" } } },
]);