Here is an example of using the aggregation API. To complicate the case, we group the case-insensitive words from the document array property.
db.articles.aggregate([ { $match: { keywords: { $not: {$size: 0} } } }, { $unwind: "$keywords" }, { $group: { _id: {$toLower: '$keywords'}, count: { $sum: 1 } } }, { $match: { count: { $gte: 2 } } }, { $sort : { count : -1} }, { $limit : 100 } ]);
which give a result, for example
{ "_id" : "inflammation", "count" : 765 } { "_id" : "obesity", "count" : 641 } { "_id" : "epidemiology", "count" : 617 } { "_id" : "cancer", "count" : 604 } { "_id" : "breast cancer", "count" : 596 } { "_id" : "apoptosis", "count" : 570 } { "_id" : "children", "count" : 487 } { "_id" : "depression", "count" : 474 } { "_id" : "hiv", "count" : 468 } { "_id" : "prognosis", "count" : 428 }
expert Oct 29 '15 at 15:37 2015-10-29 15:37
source share