mongodb-在执行$unwind时过滤掉一些值

mongodb-在执行$unwind时过滤掉一些值,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我在mongodb中有以下客户订单数据 "_id" : 7, "customer name" : "John Smith", "OrderItem" : [ { "product_category" : "Mobile", "price" : 900 }, { "product_category" : "Computer",

我在mongodb中有以下客户订单数据

 "_id" : 7,
 "customer name" : "John Smith",
 "OrderItem" : [
         {
                 "product_category" : "Mobile",
                 "price" : 900
         },
         {
                 "product_category" : "Computer",
                 "price" : 4200.48
         },
         {
                 "product_category" : "TV",
                 "price" : 670.20
         },
         {
                 "product_category" : "TV",
                 "price" : 960.52
         }
 ]
我需要对每个产品类别进行平均,如下所示:

 "_id" : 7,
 "customer name" : "John Smith",
 "OrderItem" : [
         {
                 "product_category" : "Mobile",
                 "price" : 900
         },
         {
                 "product_category" : "Computer",
                 "price" : 4200.48
         },
         {
                 "product_category" : "TV",
                 "price" : 815.36
         }
 ]

我尝试使用$unwind,但不确定如何对它们进行分组。有任何帮助吗?

对包含以下阶段的管道使用:第一个管道阶段中的操作过滤文档流,以便仅允许匹配的文档(在您的情况下为
\u id=7
的文档)未经修改地传递到下一个管道阶段,即操作。这将从输入文档中解构所需的
OrderItem
数组字段,为每个元素输出一个文档,然后您可以对其进行分组,并执行聚合操作以查找类别价格的平均值。管道中的下一个阶段是$group操作,该操作将按
产品类别
对输入文档进行分组,并将表达式应用于
价格
上的每个组。然后,最后一个阶段将重塑流中的每个文档,以生成所需的结果。因此,您的聚合将如下所示:

db.collection.aggregate([
    {
        "$match": {"_id": 7}
    },
    {   
        "$unwind": "$OrderItem"
    },
    {
        "$group": {
            "_id": "$OrderItem.product_category",
            "average_price": {
                "$avg": "$OrderItem.price"
            }
        }
    },
    {
        "$project": {
            "_id": 0,
            "product_category" : "$_id",
            "average_price": 1
        }

    }
])
结果

{
    "result" : [ 
        {
            "average_price" : 4200.48,
            "product_category" : "Computer"
        }, 
        {
            "average_price" : 815.36,
            "product_category" : "TV"
        }, 
        {
            "average_price" : 900,
            "product_category" : "Mobile"
        }
    ],
    "ok" : 1
}

首先,您应该展开
OrderItem
,然后对它们进行分组并计算平均值。下面的汇总将计算平均值

    db.collectionName.aggregate(
                    {"$match":{"customer name":"John Smith"}}, // match specified  customername  
                    {"$unwind":"$OrderItem"}, // unwind the OrderItem

                    {"$group":{"_id":"$OrderItem.product_category",
                       "avg":  {"$avg":"$OrderItem.price"} // mongo avg method used for avrage
                }}
              ).pretty()
所以上面的查询返回以下结果

{ "_id" : "Computer", "avg" : 4200.48 }
{ "_id" : "TV", "avg" : 815.36 }
{ "_id" : "Mobile", "avg" : 900 }
但上述结果与给定的预期输出不匹配,所以您应该分组两次以获得准确的输出

        db.collectionName.aggregate(
                {"$match":{"customer name":"John Smith"}},  //match given criteria
                {"$unwind":"$OrderItem"},           //unwind $OrderItem

                {"$group":{"_id":"$OrderItem.product_category",
                           "customerName":{"$first":"$customer name"}, // group all data with calculating avg
                           "id":{"$first":"$_id"}, 
                               "avg":{"$avg":"$OrderItem.price"}}},

                    {"$group":{"_id":"$id",
                       "customer Name":{"$first":"$customerName"},
                           "OrderItem":{"$push":   {"product_category":"$_id","price":"$avg"}}}} // group them for expected output

               ).pretty()
.aggregate([
{$unwind:“$OrderItem”},
{$group:{
_id:{id:$\U id],类别:$OrderItem.product\U category},
名称:{$first:$customer name},
价格:{$avg:$OrderItem.price}
}}, 
{$group:{
_id:“$\u id.id”,
OrderItem:{$push:{product_category:“$\u id.cat”,price:“$price”},
“客户名称”:{$first:$name”}
}}
])

您到底是如何尝试的?
产品类别
产品名称