使用推和组在mongodb中聚合

时间:2018-07-06 17:10:02

标签: node.js mongodb mongoose aggregation-framework

我正在尝试将两个集合进行汇总,但我不明白,我也想通过推送将所有内容分组。

这是我的传感器集合:

{
    "_id": ObjectId ("5b3e3ce0f0ed5174a3097cde"),
    "name": "co2_int",
    "type": "climate",
    "place": "interior",
    "user": ObjectId ("5b10176084195a07f63c942a"),
    "greenhouse": ObjectId ("5b1027896933440c030f8a6b"),
    "__v": 0
}

Datagreenhouses集合:

{
    "_id": ObjectId ("5b3e40d6f0ed5174a3097f4e"),
    "name": "co2_ext",
    "timestamp": ISODate ("2018-08-30T16: 25: 13.159Z"),
    "value": "200",
    "greenhouse": ObjectId ("5b1027896933440c030f8a6b"),
    "sensor": ObjectId ("5b1027dc6933440c030f8a6e"),
    "__v": 0
}

这是我的汇总:

Datagreenhouse.aggregate ([

    {"$ match": {
    "name": {$ in: arr}}},
    {
      "$ lookup": {
          "from": "sensor",
          "localField": "_id",
          "foreignField": "_id.sensor",
          "as": "sensor_type"
      }
    },


    {"$ group": {
        "_id": {
            "timestamp": "$ timestamp",
            "name": "$ name",
            "value": "$ value",
            "sensor_type": "$ sensor_type"
        },
        "Count": {"$ sum": 1}
    }},

    // {$ unwind: '$ type'},
    {"$ group": {
        "_id": "$ _id.timestamp",
        "measurements": {
            "$ push": {
                "name": "$ _id.name",
                "count": "$ Count",
                "value": "$ _id.value",
                "sensor_type": "$ _id"
            },
        },
        "count": {"$ sum": "$ Count"}
    }},
    {"$ sort": {"_id": -1}},
    {"$ limit": 1}
    ]

我需要恢复与该数据关联的传感器集合的位置。

answer:

{
            "_id": "2018-08-30T16: 25: 13.159Z",
            "measurements": [
                {
                    "name": "temperature_int",
                    "count": 1,
                    "value": "33",
                    "sensor_type": {
                        "timestamp": "2018-08-30T16: 25: 13.159Z",
                        "name": "temperature_int",
                        "value": "33",
                        "sensor_type": []
                    }
                },
                {
                    "name": "co2_int",
                    "count": 1,
                    "value": "70",
                    "sensor_type": {
                        "timestamp": "2018-08-30T16: 25: 13.159Z",
                        "name": "co2_int",
                        "value": "70",
                        "sensor_type": []
                    }
                },
                {
                    "name": "temperatura_ext",
                    "count": 1,
                    "value": "90",
                    "sensor_type": {
                        "timestamp": "2018-08-30T16: 25: 13.159Z",
                        "name": "temperatura_ext",
                        "value": "90",
                        "sensor_type": []
                    }
                }
            ],
            "count": 3
        }

我不知道如何解决这个问题。

问候,谢谢。

***** EDIT *****

我编辑帖子后无法正常工作,它总是在该位置显示相同的值

{
    "DatagreenhouseRecuperado": [
        {
            "_id": "2018-08-30T16:25:13.159Z",
            "medidas": [
                {
                    "name": "co2_ext",
                    "count": 1,
                    "value": "200",
                    "place": "interior"
                },
                {
                    "name": "co2_int",
                    "count": 1,
                    "value": "70",
                    "place": "interior"
                },
                {
                    "name": "temperatura_ext",
                    "count": 1,
                    "value": "90",
                    "place": "interior"
                },
                {
                    "name": "temperatura_int",
                    "count": 1,
                    "value": "45",
                    "place": "interior"
                }
            ],
            "count": 4
        }
    ]
}

名称为_ext时,必须将字段值放在收集传感器中的外部而不是内部。

Aggregarion:

Datagreenhouse.aggregate([

    {"$match": {
    "name" : { $in: arr }} },
    {
      "$lookup": {
          "from": "sensors",
          "localField": "sensor",
          "foreignField": "_id",
          "as": "sensor"
      }
    },
    {"$unwind":"$sensor"},
    { "$group": {
        "_id": {
            "timestamp": "$timestamp",
            "name": "$name",
            "value": "$value",
            "sensor_type": "$sensor.type"

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

    //{$unwind:'$type'},
    { "$group": {
        "_id": "$_id.timestamp",
        "medidas": {
            "$push": {
                "name": "$_id.name",
                "count": "$Count",
                "value": "$_id.value",
                //"sensor_type":"$ _id.sensor_type",
                //"sensor":"$sensor"
                "sensor_type":"$ _id.sensor_type",
                "place":"$place"
            },
        },
        "count": { "$sum": "$Count" }
    }},
    { "$sort": { "_id": -1 } },
    { "$limit": 1 }
    ]

收藏:

{
    "_id" : ObjectId("5b1027e46933440c030f8a6e"),
    "name" : "temperatura_ext",
    "type" : "clima",
    "place" : "exterior",
    "usuario" : ObjectId("5b10176084195a07f63c942a"),
    "invernadero" : ObjectId("5b1027896933440c030f8a6b"),
    "__v" : 0
}

*** EDIT02 *****

我的最终目标是:

{
            "_id": "2018-08-30T16: 25: 13.159Z",
            "measurements": [
                {
                    "name": "temperature_int",
                    "count": 1,
                    "value": "45",
                    "place": "interior"
                },
                {
                    "name": "co2_ext",
                    "count": 1,
                    "value": "200",
                    "place": "exterior"
                },
                {
                    "name": "temperatura_ext",
                    "count": 1,
                    "value": "90",
                    "place": "exterior"
                },
                {
                    "name": "co2_int",
                    "count": 1,
                    "value": "70",
                    "place": "interior"
                }
            ],
            "count": 4
        }

place字段必须是引用传感器集合的真实字段,我只需要输入该字段的值即可。现在他总是给我同样的价值。

1 个答案:

答案 0 :(得分:1)

通过切换本地字段和外部字段来调整您的查找。

$unwind可以将传感器数据从阵列减少到文档,并将传感器添加到$push

Datagreenhouse.aggregate([
  {"$match":{"name":{"$in":arr}}},
  {"$lookup":{"from":"sensor","localField":"sensor","foreignField":"_id","as":"sensor"}},
  {"$unwind":"$sensor"},
  {"$ group":{
    "_id":{
      "timestamp":"$timestamp",
      "name":"$name",
      "value":"$value",
      "sensor_type":"$sensor.type",
      "place":"$sensor.place"
    },
    "Count":{"$sum":1}
  }},
  {"$ group":{
    "_id":"$_id.timestamp",
    "measurements":{
      "$ push":{
        "name":"$_id.name",
        "count":"$Count",
        "value":"$_id.value",
        "sensor_type":"$ _id.sensor_type",
        "place":"$_id.place"
      }},
      "count":{"$sum":"$Count"}
  }},
  {"$sort":{"_id":-1}},
  {"$limit":1}
])

已更新:

Datagreenhouse.aggregate([
  {"$match":{"name":{"$in":arr}}},
  {"$lookup":{
     "from":"sensor",
      "localField":"_id",
      "foreignField":"greenhouse",
      "as":"sensor"
  }},
  {
    "$addFields": {
      "sensor": {
        "$map": {
          "input":"$sensor",
          "as": "sen",
          "in": {
            "place":"$$sen.place"
          }
        }
      }
    }
  },
  {"$group":{
    "_id":{
      "timestamp":"$timestamp",
      "name":"$name",
      "value":"$value"
    },
    "sensor":{$first:"$sensor"},
    "Count":{"$sum":1}
  }},
  {$"unwind":"$sensor"},
  {"$group":{
    "_id":"$_id.timestamp",
      "measurements":{
      "$ push":{
        "name":"$_id.name",
        "count":"$sensor",
        "value":"$_id.value",
        "place":"$_id.place"
      }},     
     "count":{"$sum":"$Count"}
  }},
  {"$sort":{"_id":-1}},
  {"$limit":1}
])