Here is a sample data object:
[
{
"_id": "56bab”,
"region": “AS”,
“spentOn”: [
“56bf623a0c90b5”
]
},
{
"_id": "57bab",
"region": "EU",
"spentOn": [
"b5”,
"b6”,
"b8”,
]
},
{
"_id": "58bab",
"region": "EU",
"spentOn": [
"b5”
"b6”
]
}
]
I am looking to create a MongoDB query (from JS) that will return the most common spentOn
value based on a specified region.
For instance, if I specify region='EU', I would like to receive back: b6, b5, and b8.
I have referenced this source for guidance and attempted the following code snippet, but I am unsure how to filter by a particular region:
return Q.promise(function(resolve, reject) {
User.aggregate( [
{ $group: { _id: "$spentOn", count: { "$sum":1 } } },
{ }
] );
resolve();
});
Any assistance on this matter would be greatly appreciated.
Thank you