简体   繁体   English

如何在mongodb中使用嵌套组?

[英]How to use nested group in mongodb?

I have one query where I want to group subdocument. 我有一个查询要分组子文档。 i have try some example but it is not working properly 我已经尝试了一些示例,但无法正常工作

Query 询问

db.getCollection("checklistCombination").aggregate([
  {
    "$lookup": {
      "from": "Users",
      "localField": "userId",
      "foreignField": "_id",
      "as": "user"
    }
  },
  {
    "$lookup": {
      "from": "checklist",
      "localField": "checklistId",
      "foreignField": "_id",
      "as": "linkChecklist"
    }
  },
  { "$unwind": "$linkChecklist" },
  {
    "$lookup": {
      "from": "orderDetail",
      "localField": "linkChecklist.product",
      "foreignField": "productRangeId",
      "as": "orderProduct"
    }
  },
  {
    "$unwind": { "path": "$orderProduct", "preserveNullAndEmptyArrays": true }
  },
  {
    "$lookup": {
      "from": "companysuppliers",
      "localField": "orderProduct.supplierId",
      "foreignField": "_id",
      "as": "comapnySupplier"
    }
  },
  {
    "$unwind": {
      "path": "$comapnySupplier",
      "preserveNullAndEmptyArrays": true
    }
  },
  {
    "$lookup": {
      "from": "suppliers",
      "localField": "comapnySupplier.supplierId",
      "foreignField": "_id",
      "as": "supplier"
    }
  },
  { "$unwind": { "path": "$supplier", "preserveNullAndEmptyArrays": true } },
  {
    "$project": {
      "_id": 1,
      "name": 1,
      "user": 1,
      "linkChecklist": 1,
      "orderProduct": 1,
      "orderProductStatusIndex": {
        "$ifNull": ["$orderProduct.statusIndex", "0"]
      },
      "comapnySupplier": 1,
      "supplier": 1
    }
  },
  { "$match": { "orderProductStatusIndex": "0" } },
  {
    "$group": {
      "_id": "$_id",
      "name": { "$first": "$name" },
      "supplier": {
        "$push": {
          "supplierId": "$supplier._id",
          "supplierName": "$supplier.name",
          "items": { "$sum": "$orderProduct.quantity" }
        }
      }
    }
  }
])

This query return below result 该查询返回结果以下

[{
    "_id" : ObjectId("5cee224b97e765079c8c2839"),
    "name" : "Dairy",
    "supplier" : [ 
        {
            "supplierId" : ObjectId("5cee12a7a01ad50f5c2229ac"),
            "supplierName" : "Bhagwandas Bherumal",
            "items" : 10
        }
    ]
},
{
    "_id" : ObjectId("5cee1a19a01ad50f5c2229f2"),
    "name" : "dairy/fruit",
    "supplier" : [ 
        {
            "supplierId" : ObjectId("5cee12a7a01ad50f5c2229ac"),
            "supplierName" : "Bhagwandas Bherumal",
            "items" : 55
        }, 
        {
            "supplierId" : ObjectId("5cee11f7a01ad50f5c2229a2"),
            "supplierName" : "Agron India PVT. LTD",
            "items" : 55
        }, 
        {
            "supplierId" : ObjectId("5cee12a7a01ad50f5c2229ac"),
            "supplierName" : "Bhagwandas Bherumal",
            "items" : 10
        }
    ]
}]

In result you can see there are two different results for Bhagwandas Bherumal in dairy/fruit (in array index 1 ). 结果,您可以看到Bhagwandas Bherumal乳制品/水果中有两个不同的结果(在数组索引1中)。 I want to group by this field and sum its items. 我想按此字段分组并汇总其项目。

Expected Result 预期结果

[
{
    "_id" : ObjectId("5cee224b97e765079c8c2839"),
    "name" : "Dairy",
    "supplier" : [
        {
            "supplierId" : ObjectId("5cee12a7a01ad50f5c2229ac"),
            "supplierName" : "Bhagwandas Bherumal",
            "items" : 10
        }
    ]
},
{
    "_id" : ObjectId("5cee1a19a01ad50f5c2229f2"),
    "name" : "dairy/fruit",
    "supplier" : [
        {
            "supplierId" : ObjectId("5cee12a7a01ad50f5c2229ac"),
            "supplierName" : "Bhagwandas Bherumal",
            "items" : 65
        },
        {
            "supplierId" : ObjectId("5cee11f7a01ad50f5c2229a2"),
            "supplierName" : "Agron India PVT. LTD",
            "items" : 55
        }
    ]
}]

Hope this solves your problem : 希望这能解决您的问题:

db.getCollection("checklistCombination").aggregate([
 {
   "$lookup": {
    "from": "Users",
    "localField": "userId",
    "foreignField": "_id",
    "as": "user"
   }
 },
 {
   "$lookup": {
    "from": "checklist",
    "localField": "checklistId",
    "foreignField": "_id",
    "as": "linkChecklist"
   }
 },
 { "$unwind": "$linkChecklist" },
 {
   "$lookup": {
     "from": "orderDetail",
     "localField": "linkChecklist.product",
     "foreignField": "productRangeId",
     "as": "orderProduct"
   }
 },
 {
   "$unwind": { "path": "$orderProduct", "preserveNullAndEmptyArrays": true }
 },
 {
   "$lookup": {
     "from": "companysuppliers",
     "localField": "orderProduct.supplierId",
     "foreignField": "_id",
     "as": "comapnySupplier"
    }
 },
 {
   "$unwind": {
    "path": "$comapnySupplier",
    "preserveNullAndEmptyArrays": true
   }
 },
 {
   "$lookup": {
     "from": "suppliers",
     "localField": "comapnySupplier.supplierId",
     "foreignField": "_id",
     "as": "supplier"
   }
 },
 { "$unwind": { "path": "$supplier", "preserveNullAndEmptyArrays": true } },
 {
   "$project": {
     "_id": 1,
     "name": 1,
     "user": 1,
     "linkChecklist": 1,
     "orderProduct": 1,
     "orderProductStatusIndex": {
        "$ifNull": ["$orderProduct.statusIndex", "0"]
      },
     "comapnySupplier": 1,
     "supplier": 1
   }
 },
 { "$match": { "orderProductStatusIndex": "0" } },
 { $group : {
    _id : {
      _id :  '$_id',
      name : '$name',
      supplierId : "$supplier._id",
      supplierName : "$supplier.name"
    },
    items  : { "$sum": "$orderProduct.quantity"}
  }
 },
 {
   $group : {
     _id : '$_id._id',
     name : { "$first": "$_id.name" },
     supplier : {
         "$push": {
            "supplierId": "$_id.supplierId",
             "supplierName": "$_id.supplierName",
             "items" : '$items'
         }
     }
   }
 }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM