首页 > 解决方案 > MongoDB中的聚合与推送

问题描述

我正在尝试在两个集合之间进行 JOIN,而且我是 MongoDB 的新手。我正在尝试通过聚合来做到这一点。

我有两个与传感器和测量相关的集合,并且在测量集合中引入了传感器集合中的两个字段。

例如,这是我的传感器集合:

{
    "_id": ObjectId ("5b4de5121852a4b3ae632db5"),
    "name": "var_ContadorRiego",
    "type": "resources",
    "place": "interior",
    "img": "img / imgriego",
    "user": ObjectId ("5b10176084195a07f63c942a"),
    "greenhouse": ObjectId ("5b1027896933440c030f8a6b"),
    "__v": 0
}

我需要在测量集合的响应中恢复 place 和 img 字段。

我的测量集合:

{
    "_id": ObjectId ("5ab5a77898492a1703b7d398"),
    "recvTime": ISODate ("2018-03-24T01: 18: 18.619Z"),
    "attrName": "var_ContadorRiego",
    "attrType": "float",
    "attrValue": "0"
}

我需要的答案是这样的:

按日期和这两个字段的聚合。

{
    "DatagreenhouseRecuperado": [
        {
            "_id": "2018-03-24T01: 18: 18.619Z",
            "measurements": [
                {
                    "attrName": "var_TempHumosCaldera",
                    "count": 1,
                    "attrValue": "17.081713",
                    "place": "interior",
                    "img": "img / imgtemphum",
                },
                {
                    "attrName": "var_TempExt",
                    "count": 1,
                    "attrValue": "12.511116",
                    "place": "outside",
                    "img": "img / imgtemp",
                },
                {
                    "attrName": "var_ContadorRiego",
                    "count": 1,
                    "attrValue": "0",
                    "place": "outside",
                    "img": "img / imgContRi",
                }
            ],
            "count": 5
        }
    ]
}

现在我只设法在没有这两个字段的情况下使用以下代码工作:     

Datagreenhouse.aggregate ([

    {"$ match": {
    "attrName": {$ in: arr}}},
    / * {
      "$ lookup": {
          "from": "sensors",
          "localField": "sensor",
          "foreignField": "attrName",
          "as": "sensor"
      }
    }, * /
    // {"$ unwind": "$ sensor"},
    {"$ group": {
        "_id": {
            "recvTime": "$ recvTime",
            "attrName": "$ attrName",
            "attrValue": "$ attrValue",
            // "sensor_type": "$ sensor.type",
            //"place":"$sensor.place "

        },
        // "sensor": {"$ first": "$ sensor"},
        //"place":{"$first":"$sensor.place "},
        "Count": {"$ sum": 1}
    }},

    // {$ unwind: '$ type'},
    {"$ group": {
        "_id": "$ _id.recvTime",
        "measurements": {
            "$ push": {
                "attrName": "$ _id.attrName",
                "count": "$ Count",
                "attrValue": "$ _id.attrValue",
                // "sensor_type": "$ _id.sensor_type",
                // "sensor": "$ sensor"
                // "sensor_type": "$ _id.sensor_type",
                // "place": "$ place"
                // "sensor_type": "$ _id.sensor_type",
                //"place":"$_id.place "
            },
        },
        "count": {"$ sum": "$ Count"}
    }},
    {"$ sort": {"_id": -1}},
    {"$ limit": 1}
    ]

正如你所看到的,我已经使用了查找和展开,但我无法让它按照我的意愿工作,除了我所展示的内容之外,它不对应,例如,如果我展示了图像和位置,但其他的传感器不是具有该名称的传感器。

我该如何解决这个问题,我被卡住了,我不出去。

问候和谢谢。

EDIT01

这是我的最终代码

数据温室.aggregate([

  {"$match":{"attrName":{"$in":arr}}},
  {"$lookup":{
    "from":"sensors",
    "localField":"attrName", // local field in measurements collection
    "foreignField":"name", //foreign field from sensors collection
    "as":"sensor"
  }},
  {"$unwind":"$sensor"},
  {"$addFields":{
    "place":"$sensor.place",// Add place to the measurements
    "img":"$sensor.img"// Add img to the measurements

  }},



  { "$group": {
      "_id": "$recvTime",
      "medidas": {
          "$push": {
              "attrName": "$attrName",
              "attrValue": "$attrValue",
              "sensor": "$sensor"

          },
      },
      "count": { "$sum": "$Count" }
  }},
  {"$sort":{"_id":-1}},
  {"$limit":1}
    ]

如何将传感器对象添加到我的两个测量领域:

现在我有这个答案:

{
    "DatagreenhouseRecuperado": [
        {
            "_id": "2018-03-24T01: 18: 18.619Z",
            "measurements": [
                {
                    "attrName": "var_ContadorRiego",
                    "attrValue": "0",
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db5",
                        "name": "var_ContadorRiego",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
                        "__v": 0
                    }
                },
                {
                    "attrName": "var_PotActBio",
                    "attrValue": "8.384025",
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db3",
                        "name": "var_PotActBio",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
                        "__v": 0
                    }
                },
                {
                    "attrName": "var_PotActInv",
                    "attrValue": "0.130222",
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db1",
                        "name": "var_PotActInv",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
                        "__v": 0
                    }
                },
                {
                    "attrName": "var_TempExt",
                    "attrValue": "12.511116",
                    "sensor": {
                        "_id": "5ab5a77898492a1703b7d3d9",
                        "name": "var_TempExt",
                        "type": "climate",
                        "place": "outside",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
                        "__v": 0
                    }
                }
            ],
            "count": 0
        }
    ]
}

有可能有这样的东西吗?

{
    "DatagreenhouseRecuperado": [
        {
            "_id": "2018-03-24T01: 18: 18.619Z",
            "measurements": [
                {
                    
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db5",
                        "name": "var_ContadorRiego",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
"attrName": "var_ContadorRiego",
                    "attrValue": "0",
                        "__v": 0
                    }
                },
                {
                    "attrName": "var_PotActBio",
                    "attrValue": "8.384025",
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db3",
                        "name": "var_PotActBio",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
"attrName": "var_ContadorRiego",
                    "attrValue": "0",
                        "__v": 0
                    }
                },
                {
                    "attrName": "var_PotActInv",
                    "attrValue": "0.130222",
                    "sensor": {
                        "_id": "5b4de5121852a4b3ae632db1",
                        "name": "var_PotActInv",
                        "type": "resources",
                        "place": "interior",
                        "img": "assets / img / temp.png",
                        "user": "5b10176084195a07f63c942a",
                        "greenhouse": "5b1027896933440c030f8a6b",
"attrName": "var_ContadorRiego",
                    "attrValue": "0",
                        "__v": 0
                    }
                }

标签: mongodbmongoose

解决方案


您可以将聚合管道简化为以下内容。

Datagreenhouse.aggregate([
  {"$match":{"attrName":{"$in":arr}}},
  {"$lookup":{
    "from":"sensors",
    "localField":"attrName", // local field in measurements collection
    "foreignField":"name", //foreign field from sensors collection
    "as":"sensor"
  }},
  {"$unwind":"$sensor"},
  {"$addFields":{
    "sensor.attrName":"$attrName",// Add attrName to the sensors
    "sensor.attrValue":"$attrValue",// Add attrValue to the sensors
  }},
  {"$group":{
    "_id":"$recvTime", // Group by time
    "measurements":{"$push":"$sensor"}, // Collect measurements
    "count":{"$sum":1} // Count measurements
  }},
  {"$sort":{"_id":-1}},
  {"$limit":1}
])

推荐阅读