MongoDB में किसी विशिष्ट मान की गणना के लिए aggregate() का उपयोग करें। पहले कलेक्शन बनाएं:
> db.demo210.insertOne({
details: [
{ ClientName: "Robert" },
{ ClientName: "John Doe" },
{ ClientName: "Robert" },
{ ClientName: "Robert" },
{ ClientName: "David Miller" }
]
});
{ "acknowledged": true, "insertedId": ObjectId("5e3d99ab03d395bdc21346f9") }
सभी डॉक्यूमेंट्स देखें:
> db.demo210.find();
आउटपुट:
{ "_id": ObjectId("5e3d99ab03d395bdc21346f9"), "details": [
{ "ClientName": "Robert" },
{ "ClientName": "John Doe" },
{ "ClientName": "Robert" },
{ "ClientName": "Robert" },
{ "ClientName": "David Miller" }
] }
विशिष्ट मान की गणना प्राप्त करने की क्वेरी:
> db.demo210.aggregate([
{ "$match": { "details.ClientName": "Robert" } },
{ "$unwind": "$details" },
{ "$match": { "details.ClientName": "Robert" } },
{ "$group": { "_id": "$_id", "Size": { "$sum": 1 } } },
{ "$match": { "Size": { "$gte": 2 } } }
]);
परिणाम:
{ "_id": ObjectId("5e3d99ab03d395bdc21346f9"), "Size": 3 }