使用push和group在mongodb中聚合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用push和group在mongodb中聚合相关的知识,希望对你有一定的参考价值。
我正在尝试对两个集合进行聚合,但是我没有得到它,我也希望通过推送对所有内容进行分组。
这是我的传感器系列:
{
"_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}
]
我需要恢复与该数据相关联的传感器集合的位置。
回答:
{
"_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
}
我不知道如何解决这个问题。
问候,谢谢。
*****编辑*****
我编辑帖子不能正常工作它始终显示相同的值的地方
{
"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,则必须将外部而非内部的字段值放置在集合传感器中。
聚合:
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字段必须是引用传感器集合的真实字段,我只需要输入该字段的值。现在他总是给我相同的价值。
答案
通过切换本地和外部字段来调整查找。
$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}
])
以上是关于使用push和group在mongodb中聚合的主要内容,如果未能解决你的问题,请参考以下文章
MongoDB $lookup 和 $group 返回空数组
MongoDb 聚合在 $group 中使用 $sortByCount