I am trying to count the number of occurrences of a string in an array in my collection using Mongoose. My โschemaโ looks like this:
var ThingSchema = new Schema({
tokens: [ String ]
});
My goal is to get the top 10 โtokensโ in the โThingโ collection, which can contain multiple values โโfor each document. For instance:
var documentOne = {
_id: ObjectId('50ff1299a6177ef9160007fa')
, tokens: [ 'foo' ]
}
var documentTwo = {
_id: ObjectId('50ff1299a6177ef9160007fb')
, tokens: [ 'foo', 'bar' ]
}
var documentThree = {
_id: ObjectId('50ff1299a6177ef9160007fc')
, tokens: [ 'foo', 'bar', 'baz' ]
}
var documentFour = {
_id: ObjectId('50ff1299a6177ef9160007fd')
, tokens: [ 'foo', 'baz' ]
}
... would give me the result of the data:
[ foo: 4, bar: 2 baz: 2 ]
I am considering using MapReduce and Aggregate for this tool, but I'm not sure if this is the best option.
source
share