Mongo: count the number of word occurrences in a set of documents

前端 未结 4 806
北荒
北荒 2020-12-08 16:19

I have a set of documents in Mongo. Say:

[
    { summary:\"This is good\" },
    { summary:\"This is bad\" },
    { summary:\"Something that is neither good         


        
4条回答
  •  孤城傲影
    2020-12-08 17:04

    You can use #split. Try Below query

    db.summary.aggregate([
    { $project : { summary : { $split: ["$summary", " "] } } },
    { $unwind : "$summary" },
    { $group : { _id:  "$summary" , total : { "$sum" : 1 } } },
    { $sort : { total : -1 } }
    ]);
    

提交回复
热议问题