In my code, I have an aggregate function that counts the occurrences of a value in the database:
let data: any = await this.dataModel.aggregate(
[
{
$match: {
field: new ObjectID(fieldID),
},
},
{
$group: {
_id: "$value",
total_for_value: { $sum: 1 },
},
},
]
);
While this solution is effective, my data structure presents a challenge. There are two types of value fields. Some are structured like this:
{
"_id" : ObjectId("123"),
"value" : "MALE"
}
and others like this:
{
"_id" : ObjectId("456"),
"value" : {
"value" : "MALE",
}
}
I need to find a way to group records where both the _id and _id.value are the same. Currently, they are being counted separately.