Javascript 无法使用node.js根据MongoDB中的特定字符串对值进行分组?

Javascript 无法使用node.js根据MongoDB中的特定字符串对值进行分组?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有一个用例,其中我有3个集合。i、 e、模板、组和每个模板的1-动态集合,将在创建模板后立即创建 每个模板由设备组成,每个设备由组集合的组ID组成。动态收集存储模板设备推送的数据 以下是我收集的样本数据 db.getCollection('13435158964').aggregate([ { "$match": {"entryDayTime":{ $lt: new Date("2019-11-29T18:30:00.000Z"),

我有一个用例,其中我有3个集合。i、 e、模板、组和每个模板的1-动态集合,将在创建模板后立即创建

每个模板由
设备
组成,每个
设备
由组集合的
组ID
组成。动态收集存储模板设备推送的数据

以下是我收集的样本数据

db.getCollection('13435158964').aggregate([
{ "$match": {"entryDayTime":{
                    $lt: new Date("2019-11-29T18:30:00.000Z"),
                    $gte: new Date("2019-10-31T18:30:00.000Z")
                }
}
},{
    $lookup:{
        from:"templates",
        localField:"template",
        foreignField:"templateId",
        as:"templateData"
    }
},{
    $unwind:"$templateData"
},{
    $unwind:"$templateData.devices"
},{
    $lookup:{
       from:"groups",
       localField:"templateData.devices.group",
       foreignField:"_id",
       as:"groupData"
     }
},{
    $unwind:"$groupData"
},{
    $group:{
       _id: "$groupData.groupName",
       heat:{$sum:"$heat"},
       humidity:{$sum:"$humidity"},
       count:{$sum:1}
    }
},{
    $project:{
       "count":1,"heat":1,"humidity":1,"templateData.devices.group":1,"templateData.devices.deviceId":1,"groupData.groupName":1
}
}])
模板数据

{
  "_id": "5e0ae38729218b0a3861118b",
  "templateId": "13435158964",
  "devices": [
    {
      "deviceId": "a-1",
      "_id": "5e0ae49629218b0a3861118f",
      "group": "5e0ae41d29218b0a3861118d",
    },{
       "deviceId": "sb-0001",
       "_id": "5e0af166981f39410cd89b72",
       "group": "5e0af11d981f39410cd89b70"
    }]
}
[
  {
      "_id": "5e0ae793b1384737a4f855cf",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 30,
      "humidity": 40
    },
    {
      "_id": "5e0ae7a2b1384737a4f855d0",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 40,
      "humidity": 20
    },
    {
      "_id": "5e0ae890b1384737a4f855d3",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 10,
      "humidity": 20
    },
    {
      "_id": "5e0af188981f39410cd89b73",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 60,
      "humidity": 50
    },
    {
      "_id": "5e0af196981f39410cd89b74",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 15,
      "humidity": 25
    }]
[{
   "_id": "5e0af11d981f39410cd89b70",
   "template": "5e0ae38729218b0a3861118b",
   "groupName": "Flats"
 },{
    "_id": "5e0ae41d29218b0a3861118d",
    "template": "5e0ae38729218b0a3861118b",
    "groupName": "Swimming Pool"
 }]
动态采集数据

{
  "_id": "5e0ae38729218b0a3861118b",
  "templateId": "13435158964",
  "devices": [
    {
      "deviceId": "a-1",
      "_id": "5e0ae49629218b0a3861118f",
      "group": "5e0ae41d29218b0a3861118d",
    },{
       "deviceId": "sb-0001",
       "_id": "5e0af166981f39410cd89b72",
       "group": "5e0af11d981f39410cd89b70"
    }]
}
[
  {
      "_id": "5e0ae793b1384737a4f855cf",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 30,
      "humidity": 40
    },
    {
      "_id": "5e0ae7a2b1384737a4f855d0",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 40,
      "humidity": 20
    },
    {
      "_id": "5e0ae890b1384737a4f855d3",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 10,
      "humidity": 20
    },
    {
      "_id": "5e0af188981f39410cd89b73",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 60,
      "humidity": 50
    },
    {
      "_id": "5e0af196981f39410cd89b74",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 15,
      "humidity": 25
    }]
[{
   "_id": "5e0af11d981f39410cd89b70",
   "template": "5e0ae38729218b0a3861118b",
   "groupName": "Flats"
 },{
    "_id": "5e0ae41d29218b0a3861118d",
    "template": "5e0ae38729218b0a3861118b",
    "groupName": "Swimming Pool"
 }]
集团数据

{
  "_id": "5e0ae38729218b0a3861118b",
  "templateId": "13435158964",
  "devices": [
    {
      "deviceId": "a-1",
      "_id": "5e0ae49629218b0a3861118f",
      "group": "5e0ae41d29218b0a3861118d",
    },{
       "deviceId": "sb-0001",
       "_id": "5e0af166981f39410cd89b72",
       "group": "5e0af11d981f39410cd89b70"
    }]
}
[
  {
      "_id": "5e0ae793b1384737a4f855cf",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 30,
      "humidity": 40
    },
    {
      "_id": "5e0ae7a2b1384737a4f855d0",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 40,
      "humidity": 20
    },
    {
      "_id": "5e0ae890b1384737a4f855d3",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 10,
      "humidity": 20
    },
    {
      "_id": "5e0af188981f39410cd89b73",
      "template": "13435158964",
      "deviceId": "a-1",
      "heat": 60,
      "humidity": 50
    },
    {
      "_id": "5e0af196981f39410cd89b74",
      "template": "13435158964",
      "deviceId": "sb-0001",
      "heat": 15,
      "humidity": 25
    }]
[{
   "_id": "5e0af11d981f39410cd89b70",
   "template": "5e0ae38729218b0a3861118b",
   "groupName": "Flats"
 },{
    "_id": "5e0ae41d29218b0a3861118d",
    "template": "5e0ae38729218b0a3861118b",
    "groupName": "Swimming Pool"
 }]
因此,对于上述数据,我试图获得指定的
groupName
的热量和湿度之和。 因此,预期产出应为:

预期产出

[
  {
    "_id": "Swimming Pool",
    "heat": 110,
    "humidity": 165,
    "count":2
  },{
    "_id": "Flat",
    "heat": 70,
    "humidity": 80,
    "count":1
  }]
**返回输出**

[
  {
    "_id": "Swimming Pool",
    "heat": 180,
    "humidity": 245,
    "count":2
  },{
    "_id": "Flat",
    "heat": 180,
    "humidity": 245,
    "count":1
  }]

我不知道如何实现这一点,有人能帮我解决这个问题吗

我的查询: 在这里,模板和组是不同的集合

db.getCollection('13435158964').aggregate([
{ "$match": {"entryDayTime":{
                    $lt: new Date("2019-11-29T18:30:00.000Z"),
                    $gte: new Date("2019-10-31T18:30:00.000Z")
                }
}
},{
    $lookup:{
        from:"templates",
        localField:"template",
        foreignField:"templateId",
        as:"templateData"
    }
},{
    $unwind:"$templateData"
},{
    $unwind:"$templateData.devices"
},{
    $lookup:{
       from:"groups",
       localField:"templateData.devices.group",
       foreignField:"_id",
       as:"groupData"
     }
},{
    $unwind:"$groupData"
},{
    $group:{
       _id: "$groupData.groupName",
       heat:{$sum:"$heat"},
       humidity:{$sum:"$humidity"},
       count:{$sum:1}
    }
},{
    $project:{
       "count":1,"heat":1,"humidity":1,"templateData.devices.group":1,"templateData.devices.deviceId":1,"groupData.groupName":1
}
}])
一个简单的阶段应该可以做到这一点:

db.collection.aggregate([
    {
        $group: {
            _id: "$groupData.groupName",
            heat: {$sum: "$heat"},
            humidity: {$sum: "$humidity"},
            count: {$sum: 1}
        }
    }
])

您可以使用mongo管道框架的
$group
阶段。像这样的东西应该有用

db.collection.aggregate([{
    "$group": {
        "_id": "$groupData.groupName",
        "humidity": {
            "$sum": "$humidity"
        },
        "heat": {
            "$sum": "$heat"
        },
        "count": {
            "$sum": 1
        }
    }
}])

据我所知,您希望数据按组名分组。但是你得到了错误的结果

因此,请参考以下聚合代码

db.getCollection('13435158964').aggregate([
  {
    $lookup: {
      from: "templates",
      localField: "template",
      foreignField: "templateId",
      as: "templateData"
    }
  },
  {
    $unwind: "$templateData"
  },
  {
    $unwind: "$templateData.devices"
  },
  {
    $match: {
      $expr: {
        $eq: [
          "$deviceId",
          "$templateData.devices.deviceId"
        ]
      }
    }
  },
  {
    $lookup: {
      from: "groups",
      localField: "templateData.devices.group",
      foreignField: "_id",
      as: "groupData"
    }
  },
  {
    $unwind: "$groupData"
  },
  {
    $group: {
      _id: "$groupData.groupName",
      heat: {
        "$sum": "$heat"
      },
      humidity: {
        "$sum": "$humidity"
      },
      count: {
        "$sum": 1
      }
    }
  }
])
你会得到这样的答案

[
  {
    "_id": "Swimming Pool",
    "count": 3,
    "heat": 100,
    "humidity": 110
  },
  {
    "_id": "Flats",
    "count": 2,
    "heat": 55,
    "humidity": 45
  }
]

您也可以参考此链接,

无法对值进行分组…:有什么问题?下面是一个关于如何使用
$group
计算和求和的示例:“模板和组是不同的集合。”:请发布这两个集合的结构(并为它们命名)。@prasad\uu我已经更新了问题,请现在检查。@prasad\uu请检查这个问题。我已经尝试过了,但它给出了重复的值。对于ex,如果我们取扁平组的
,它也给出了所有其他组的
之和。i、 所有组的总热量都显示在每个组中。这是不可能的,我建议您查看原始数据,以验证您需要的结果是否与您得到的结果不同。如果不查看数据,很难判断,但使用您当前的查询,您可能会“查找”同一文档的多个实例,这可能是您无法获得预期结果的原因。请检查此项。它按预期工作,所有数据共享相同的模板ID,这意味着它们将得到完全相同的复制。问题不在于团队,而在于如何构建数据。