Mongo average aggregation query with no group

前端 未结 3 1591
生来不讨喜
生来不讨喜 2020-12-03 21:39

I am trying to get the average of a whole field using the aggregation framework in Mongo. However i can\'t seem to find any example that uses it without a group parameter. <

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 22:38

    So if your data actually was numeric which is it not and your intention is to exclude the documents that have a "greater than zero" value then you include a $match statement in your aggregation pipeline in order to "filter" out these documents:

    db.EvaluatedSentiments.aggregate([
        { "$match": {
            "bvc": { "$gt": 0 }
        }},
        { "$group": {
            "_id": null,
            "bvc": { "$avg": "$bvc" }
        }}
    ])
    

提交回复
热议问题