我试图在两个集合之间建立一个JOIN,而我是MongoDB的新手。我正在尝试通过聚合来实现。
我有两个与传感器和测量相关的集合,在度量集合中引入了两个在传感器集合中的字段。
例如,这是我的传感器集合:
PaneFooter
需要在测量集合的响应中恢复位置和img字段的地方。
我收集的测量值:
{
"_id": ObjectId ("5b4de5121852a4b3ae632db5"),
"name": "var_ContadorRiego",
"type": "resources",
"place": "interior",
"img": "img / imgriego",
"user": ObjectId ("5b10176084195a07f63c942a"),
"greenhouse": ObjectId ("5b1027896933440c030f8a6b"),
"__v": 0
}
我需要这样的答案:
按日期和这两个字段进行汇总。
{
"_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
}
]
}
如您所见,我已经使用了查找和展开功能,但是我无法使其按需工作,除了我所显示的内容外,例如,如果我显示了图像和位置,但是其他传感器的名称不一样。
我怎么解决这个问题,我被卡住了,我没有离开。
问候,谢谢。
EDIT01
这是我的最终代码
Datagreenhouse.aggregate([
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}
]
如何在两个测量字段中添加传感器对象:
现在我有这个答案:
{"$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
}
]
}
答案 0 :(得分:1)
您可以在下面简化聚合管道。
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}
])