代码之家  ›  专栏  ›  技术社区  ›  Manolait

mongodb中的push聚合

  •  0
  • Manolait  · 技术社区  · 7 年前

    我正在尝试在两个集合之间建立连接,我对MongoDB还不熟悉我正试图通过聚合的方式来做到这一点。

    我有两个与传感器和测量相关的集合,在度量集合中介绍了传感器集合中的两个字段。

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

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

    我需要恢复测量采集响应中的位置和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}
        ]
    

    如你所见,我已经使用了查找和展开,但我不能让它按我所希望的方式工作,此外,我所显示的并不对应,例如,如果我已经显示了图像和位置,但其他传感器没有显示该名称的传感器。

    我怎么能解决这个问题,我被困住了,不能出去。

    你好,谢谢你。

    编辑01

    这是我最后的密码

    数据温室。聚合([

      {"$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
                        }
                    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   s7vr    7 年前

    您可以将聚合管道简化到下面。

    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}
    ])