'How to find Distinct No of Document on specific field MongoDB and Implement in Java
I have data and need to group by two fields and in response display unique filed along with the count on this field.
Sample data:
[
{
"categoryCode":"categoryCode1",
"categoryName":"categoryName1",
"cat_id": "A"
},
{
"categoryCode":"categoryCode1",
"categoryName":"categoryName1",
"cat_id": "B"
},
{
"categoryCode":"categoryCode2",
"categoryName":"categoryName2",
"cat_id": "C"
}
]
Expected Output:
[
{
"categoryCode":"categoryCode1",
"categoryName":"categoryName1",
"cat_count": 2
},
{
"categoryCode":"categoryCode2",
"categoryName":"categoryName2",
"cat_count": 1
}
]
and how to implement it in Java with Aggregation Object.
GroupOperation groupOperation = Aggregation.group("categoryCode", "categoryName").sum(1).as("cat_count");
but getting compile error:
1 is not taken inside sum method
Solution 1:[1]
$group
- Group bycategoryCode
andcategoryName
. And add aggregate fieldcat_count
for$sum
when match with group.$project
- Display document for desired output field.
db.collection.aggregate([
{
$group: {
_id: {
"categoryCode": "$categoryCode",
"categoryName": "$categoryName"
},
"cat_count": {
$sum: 1
}
}
},
{
$project: {
"_id": 0,
"categoryCode": "$_id.categoryCode",
"categoryName": "$_id.categoryName",
"cat_count": "$cat_count"
}
}
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yong Shun |