如何为零匹配的文档指定零?

时间:2016-12-10 08:18:53

标签: mongodb aggregation-framework

我有一个像这样的json文件:

{
 "ID": 1,
 "FIELD1": "1",
 "Comments": "brakes, good, condition.  ",
 "FIELD3": "73"
},
{
 "ID": 2,
 "FIELD1": "2",
 "Comments": "Smooth, total overall very good",
 "FIELD3": "33"
}, 

{
 "ID": 3,
 "FIELD1": "20",
 "Comments": "Nothing",
 "FIELD3": "3"
 }

我使用聚合函数计算单词数(例如good),并获得此输出:

_id                                   Count
brakes, good condition                  3
Smooth, total overall very good         4

我的问题是有评论返回零匹配。我如何得到这个输出:

_id                                   Count
brakes, good condition                  3
Smooth, total overall very good         4
Nothing                                 0

我的疑问是:

  db.test.aggregate
  ([
  {$match: {$or: [
                  {"Comments" : {$regex : /.*smooth.*/i}},   
                  {"Comments" : {$regex : /.*good.*/i}},
                  {"Comments" : {$regex : /.*great.*/i}}
                  ]
           }
  },

  {$group: {_id : "$Comments", Count: {$sum : 1},}},

  ])

0 个答案:

没有答案