MongoDB $group之后的子集使用$group

MongoDB Use $group for the subset after $group

我刚学会 mongoDB,我正试图通过我的客户数据库查找一些回头客信息。 样本集合:

{
"_id" : ObjectId("5b7617e48146d8bae"),
"amazon_id" : "112",
"date" : "2018-01-25T18:40:55-08:00",
"email" : "xxxxx@marketplace.amazon.com",
"buy_name" : "xxxxx",
"sku" : "NPC-50",
"qty" : 8,
"price" : 215.92,
"reci_name" : "XXXXX",
"street1" : "XXXXX",
"street2" : "",
"street3" : "",
"city" : "XXXXX",
"state" : "XXXXX",
"zip_code" : "XXXXXX"
 }

{
"_id" : ObjectId("5b761712e48146d8bae"),
"amazon_id" : "114",
"date" : "2018-01-27T18:40:55-08:00",
"email" : "xxxxx@marketplace.amazon.com",
"buy_name" : "xxxxx",
"sku" : "ABC",
"qty" : 1,
"price" : 19.99,
"reci_name" : "XXXXX",
"street1" : "XXXXX",
"street2" : "",
"street3" : "",
"city" : "XXXXX",
"state" : "XXXXX",
"zip_code" : "XXXXXX"
 }

我按电子邮件 ID 对所有客户信息进行分组,这是我的代码:

db.getCollection('order').aggregate([

 { $group: { _id:  "$email", 
             OrderInfo: {$push: {orderId: "$amazon_id", sku: "$sku", qty: "$qty", price:"$price"
                                    }},
             CustomerInfo: {$addToSet: {buyName: "$buy_name",reName: "$reci_name", email: "$email", street1: "$street1",
                street2: "$street2", city: "$city", state: "$state", zipCode: "$zip_code"} }
             }},

 { $project: {_id: 1, OrderInfo: 1, CustomerInfo:1, total_price:{$sum: "$OrderInfo.price"}   }},
 { $match: {total_price: {$gt:100} } },
 { $sort: {total_price:-1}},

], { allowDiskUse: true }  );

显示结果:

 {
"_id" : "xxxxxxx@marketplace.amazon.com",
"OrderInfo" : [ 
    {
        "orderId" : "112",
        "sku" : "NPC-50",
        "qty" : 8,
        "price" : 215.92
    }, 
    {
        "orderId" : "112",
        "sku" : "NPC-50",
        "qty" : 1,
        "price" : 26.99
    }, 
    {
        "orderId" : "114",
        "sku" : "NPC-50",
        "qty" : 1,
        "price" : 26.99
    }, 
    {
        "orderId" : "114",
        "sku" : "ABC",
        "qty" : 1,
        "price" : 19.99
    }, 
    {
        "orderId" : "116",
        "sku" : "ABC",
        "qty" : 1,
        "price" : 19.99
    }, 
],
"CustomerInfo" : [ 
    {
        "buyName" : "xxxxxxxxx",
        "reName" : "xxxxxxxxxxxx",
        "email" : "xxxxxxxxxxxx@marketplace.amazon.com",
        "street1" : "xxxxxxxxxxx",
        "street2" : "",
        "city" : "xxxxxxxxxx",
        "state" : "xxxxxxxxxxxx",
        "zipCode" : "xxxxxxxxxx"
    }, 
    {
        "buyName" : "xxxxxxxxxx",
        "reName" : "xxxxxx",
        "email" : "xxxxxxxx@marketplace.amazon.com",
        "street1" : "xxxxxxxxxxx",
        "street2" : "",
        "city" : "xxxxx",
        "state" : "xxxx",
        "zipCode" : "xxxxxxxx"
    }
],
"total_price" : 309.88
}

但是,我想对sku进行分组,并在OrderInfo Set中汇总数量和价格。我的预期输出是这样的:

     {
"OrderInfo" : [ 
    {
        "sku": "NPC-50",
        "qty": 10,
        "price": 269.9
    },
    {
        "sku": "ABC",
        "qty": 2,
        "price": 39.98
    },
],
"CustomerInfo" : [ 
    {
        "buyName" : "xxxxxxxxx",
        "reName" : "xxxxxxxxxxxx",
        "email" : "xxxxxxxxxxxx@marketplace.amazon.com",
        "street1" : "xxxxxxxxxxx",
        "street2" : "",
        "city" : "xxxxxxxxxx",
        "state" : "xxxxxxxxxxxx",
        "zipCode" : "xxxxxxxxxx"
    }, 
    {
        "buyName" : "xxxxxxxxxx",
        "reName" : "xxxxxx",
        "email" : "xxxxxxxx@marketplace.amazon.com",
        "street1" : "xxxxxxxxxxx",
        "street2" : "",
        "city" : "xxxxx",
        "state" : "xxxx",
        "zipCode" : "xxxxxxxx"
    }
],
"total_price" : 309.88
}

任何帮助将不胜感激。

您可以尝试以下聚合

db.collection.aggregate([
  { "$group": {
    "_id": {
      "email": "$email",
      "sku": "$sku"
    },
    "CustomerInfo": {
      "$addToSet": {
        "buyName": "$buy_name",
        "otherFields": "$otherFields",
      }
    },
    "price": { "$sum": "$price" },
    "qty": { "$sum": "$qty" }
  }},
  { "$group": {
    "_id": "$_id.email",
    "CustomerInfo": { "$first": "$CustomerInfo" },
    "OrderInfo": {
      "$push": {
        "sku": "$_id.sku",
        "qty": "$qty",
        "price": "$price"
      }
    }
  }}
])

您可以使用以下聚合。

db.order.aggregate([
 {"$group":{
   "_id":{"email":"$email","sku":"$sku"},
   "qty":{"$sum":"$qty"},
   "price":{"$sum":"$price"},
   "CustomerInfo":{
    "$addToSet":{
      "buyName":"$buy_name",
      "reName":"$reci_name",
      "email":"$email",
      "street1":"$street1",
      "street2":"$street2",
      "city":"$city",
      "state":"$state",
      "zipCode":"$zip_code"
     }
   }
 }},
  {"$group":{
   "_id":"$_id.email",
   "OrderInfo":{"$push":{"sku":"$_id.sku","qty":"$qty","price":"$price"}},
   "total_price":{"$sum":"$price"},
   "CustomerInfo":{"$first":"$CustomerInfo"}
 }},
 {"$match":{"total_price":{"$gt":100}}},
 {"$sort":{"total_price":-1}}
])