Is there a mongoose function in order to find a field value and the amount of times it exists in the document.
For example:
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "400000"
},
I need the function to return something like:
bin: 411111, count: 3 bin: 400000, count: 1
CodePudding user response:
You can use the following aggregation pipeline to achieve exactly what you want:
.aggregate([
{
$group: {
_id: "$Info.bin",
count: {
$sum: 1
}
}
}
])