mongodb 没有组的 Mongo 平均聚合查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23323515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:41:45  来源:igfitidea点击:

Mongo average aggregation query with no group

mongodbaggregation-framework

提问by NicolasZ

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.

我正在尝试使用 Mongo 中的聚合框架来获取整个领域的平均值。但是,我似乎找不到任何使用它而没有 group 参数的示例。

I have the following document structure:

我有以下文档结构:

 {
      "_id" : ObjectId("5352703b61d2739b2ea44e4d"),
      "Semana" : "2014-02-23 - 2014-03-01",
      "bolsaDeValores" : "7",
      "bvc" : "8",
      "dollar" : "76",
      "ecopetrol" : "51",
      "dollarPrice" : "18"
 }

Basically what i want to do is get the average value of the bvc field, and any other numeric one, for the whole collection in the fastest possible way (without using MapReduce as it is less efficient than the Aggregation Framework).

基本上我想要做的是以最快的方式获取整个集合的 bvc 字段和任何其他数字的平均值(不使用 MapReduce,因为它比聚合框架效率低)。

I have tried to group on a greater than zero basis as well but to no avail:

我也尝试在大于零的基础上分组,但无济于事:

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

I appreciate any help you could provide.

我很感激你能提供的任何帮助。

References: Mongo aggregation manual

参考资料:Mongo 聚合手册

回答by Sebastian

First of all store numerical values as numbers. Afterwards you can use a simple statement to calculate the average:

首先,将数值存储为数字。之后,您可以使用一个简单的语句来计算平均值:

db.collection.aggregate({ 
  "$group": {
    "_id": null, 
    "avg_bvc": { "$avg": "$bvc" } 
  } 
})

You can simply use more $avgaggregation operators to get averages for your other numeric fields:

您可以简单地使用更多$avg聚合运算符来获取其他数字字段的平均值:

db.collection.aggregate({ 
  "$group": {
    "_id": null, 
    "avg_bvc": { "$avg": "$bvc" }, 
    "avg_dollar": { "$avg": "$dollar" } 
  } 
})

回答by Neil Lunn

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 $matchstatement in your aggregation pipeline in order to "filter" out these documents:

因此,如果您的数据实际上是数字,事实并非如此,并且您的意图是排除具有“大于零”值的文档,那么您可以$match在聚合管道中包含一条语句以“过滤”这些文档:

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

回答by ABHISHEK GUPTA

For more details please visit the following link: https://docs.mongodb.com/manual/reference/operator/aggregation/group/index.html

db.EvaluatedSentiments.aggregate([
{
$group:{_id:null,avgbvc: {$avg:"$bvc"}}
}
]).forEach(printjson)