我想使用 Node-RED 从 MongoDB 中提取价值。在下面提到的流程中,运行Raspberry Pi的RFID阅读器读取 RFID(例如:" badgeID":12)并使用MQTT产生价值,并且在Node-RED中订阅相同的值,并将此订阅值传递给Node-RED中的 MongoDB节点,以便根据此badgeID过滤值。在MongoDB数据库中,一个文档的结构看起来像
{ "_id": "5631a6ba7de98c4a497dfdcb", "badgeID": "1", "tempValue": 25,
"unitofMeasurement": "C" }
。为此,我在Node-RED中的流程如下:
[{"id":"3015a0de.cfea6","type":"mongodb","z":"6ab069e2.954f98","hostname":"127.0.0.1",
"port":"27017","db":"iotsuiteuser","name":"ProfileDB"},{"id":"5a6e14aa.a591ec","type":"mqtt-
broker","z":"6ab069e2.954f98","broker":"test.mosquitto.org","port":"1883","clientid":"",
"usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,
"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","birthTopic":"","birthQos
":"0","birthRetain":"false","birthPayload":""},{"id":"2ec6f370.d1390c","type":"mqtt in","z":"6ab069e2.954f98","name":"RFID Reader Subscriber","topic":"badgeDetected","broker":"5a6e14aa.a591ec","x":137,"y":151,"wires":[["fc22a30d.03dd6"]]},{"id":"fc22a30d.03dd6","type":"function","z":
"6ab069e2.954f98","name":"Proximity","func":"sensorMeasurement=JSON.parse(msg.payload);\nmsg.payload=sensorMeasurement.badgeID;
\nreturn msg;\n","outputs":1,"noerr":0,"x":365,"y":188,"wires":[["48d08989.b72f78"]]},{"id":
"bb7c3bfd.4483c8","type":"debug","z":"6ab069e2.954f98","name":"Debug","active":true,"console":
"false","complete":"payload[0].tempValue","x":930,"y":221,"wires":[]},{"id":
"48d08989.b72f78","type":"mongodb in","z":"6ab069e2.954f98","mongodb":"3015a0de.cfea6","name":"ProfileDB","collection":"ProfileDB","operation":"find","x":613,"y":191,"wires":[["bb7c3bfd.4483c8"]]}]
Node-RED调试控制台中的输出显示如下:
[ { "_id": "5631a6ba7de98c4a497dfdcb", "badgeID": "1", "tempValue": 25,
"unitofMeasurement": "C" }, { "_id": "5631a6cd7de98c4a497dfdcc", "badgeID": "2",
"tempValue": 28, "unitofMeasurement": "C" }, { "_id": "5631b84023f7c97bc1044178", "badgeID": "3", "tempValue": 31, "unitofMeasurement": "c" }, { "_id": "565175ded239daf9794b1d48", "badgeID":
"4", "tempValue": 24, "unitOfMeasurement": "C" }, { "_id": "567287cc5e18a39f297395d6",
"badgeID": "69d65035", "tempValue": 33, "unitofMeasurement": "C" } ]
我想选择tempValue,其中badgeID =" 69d65035"。这里的问题是它显示给定集合中的所有文档。这该怎么做 ?我走错了路吗? MongoDB数据库中的文档片段如下:
答案 0 :(得分:2)
您的函数节点包含以下内容:
sensorMeasurement=JSON.parse(msg.payload);
msg.payload=sensorMeasurement.badgeID;
return msg;
这将只发送"69d65030"
到mongo节点,它不知道文档中哪个字段与此匹配。
Mongo find doc说你需要传递和反对用于过滤器,所以要匹配badgeID,你需要有效载荷看起来像这样:
{ badgeID: '69d65030'}
要执行此操作,该功能应如下所示:
sensorMeasurement=JSON.parse(msg.payload);
msg.payload={ badgeID: sensorMeasurement.badgeID};
return msg;
答案 1 :(得分:0)
最后我找出了答案。 Proximity 节点的逻辑如下:
sensorMeasurement=JSON.parse(msg.payload);
msg.payload={ badgeID: sensorMeasurement.badgeID};
msg.projection = {'_id' : 0,'tempValue':1};
return msg;
上面的代码只从数据库中获取tempValue。