Search code examples
node.jsmongodbiotnode-red

How to extract value from MongoDB using Node-RED?


I would like to extract value from MongoDB using Node-RED. Here in the below mentioned flow, RFID Reader running of Raspberry Pi reads RFID(ex:"badgeID":12) and produce value using MQTT, and same value is subscribe in Node-RED and this subscribe value is passed to MongoDB node in Node-RED in order to filter value based on this badgeID. In MongoDB database, structure of one document is looks like

{ "_id": "5631a6ba7de98c4a497dfdcb", "badgeID": "1", "tempValue": 25,
"unitofMeasurement": "C" }

. For that my flow in Node-RED is as follows:

[{"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"]]}]

Output display in the Node-RED debug console is as follows:

[ { "_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" } ]

I want to select tempValue where badgeID="69d65035". Here the problem is it displays all the document in the given collection. How to do this ? Am i going on wrong path ? Snippet of document in the MongoDB database is as follows: enter image description here


Solution

  • Your function node contains the following:

    sensorMeasurement=JSON.parse(msg.payload);
    msg.payload=sensorMeasurement.badgeID;
    return msg;
    

    This is going to send just "69d65030" to the mongo node, which it won't have a clue which field in the document to match this to.

    The Mongo find doc says you need to pass and object to use for the filter, so to match the badgeID you would need the payload to look something like this:

    { badgeID: '69d65030'}
    

    To do this the function should look like this:

    sensorMeasurement=JSON.parse(msg.payload);
    msg.payload={ badgeID: sensorMeasurement.badgeID};
    return msg;