Reputation: 718
I have a mongo collection of searches. Each search has a criteria object, which can have any combination of criteria. So something like:
{
"_id": 1,
"criteria": {
"state": ["NY", "IL"]
...
},
...
}
I'm building a mongo aggregation pipeline, and I'm wondering how to project only the keys so that I can count them.
So far the first step of my pipeline is:
db.userSearch.aggregate([
{ "$project": { "criteria":1 } },
...
])
This returns all of the criteria objects correctly, now I need to project the keys somehow. Does anyone have any ideas?
Edit:
desired output: {"state":20, "balance":5, "geolocation":10, ...}
Upvotes: 0
Views: 196
Reputation: 718
In case anyone was wondering, I used mapReduce as follows.
map = function() {
Object.keys(this.criteria).forEach(function(k) {
emit(k, 1)
})
}
reduce = function(k, vals) {
return Array.sum(vals)
}
db.userSearch.mapReduce(map, reduce, 'out')
Upvotes: 1