Say I have a collection with documents like the following:
{
title: "First",
sub_items: ['test', 'another test', 'whatever]
},
{
title: "Second",
sub_items: ['something', 'something else']
}
And I would like to get output like below:
{"First", 3},
{"Second, 2}
What is the best way to do this? I have implemented it using the map
function like below:
db.coll.find().map(
function(item)
{
return {title: item.title, count: item.sub_items.length};
}
);
When attempting to get the above results, would I be better off from a performance perspective to try and use something like aggregate or mapReduce?