Node-red mongodb update record

Hi friends,

I connect to the node-red server with mqtt and send a json object as follows.
{"nodebolge": "tttttt", "nodename": "device5", "nodeabone": "144", "index": "655477"}

If you have the same record with the "nodename" field, how can I update that record.

[{"id":"82480c90.eb63c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"198a0d21.d320f3","type":"mqtt in","z":"82480c90.eb63c","name":"","topic":"/deneme/#","qos":"0","broker":"20b98e5f.a35d02","x":80,"y":100,"wires":[["e7872a27.108a38"]]},{"id":"ed7155f2.0b3398","type":"influxdb out","z":"82480c90.eb63c","influxdb":"bff5e425.ff6f68","name":"","measurement":"nodes","precision":"","retentionPolicy":"","x":680,"y":80,"wires":},{"id":"e7872a27.108a38","type":"json","z":"82480c90.eb63c","name":"","property":"payload","action":"","pretty":false,"x":210,"y":100,"wires":[["3f92a06b.8a235","f9f53243.c7b2e","4e20cb46.5ae044"]]},{"id":"3f92a06b.8a235","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":390,"y":120,"wires":},{"id":"f9f53243.c7b2e","type":"function","z":"82480c90.eb63c","name":"MQTT to InfluxDB","func":"\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":80,"wires":[["9b5c50ba.dbe6e","ed7155f2.0b3398","b9655963.9c5818"]]},{"id":"9b5c50ba.dbe6e","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":40,"wires":},{"id":"b9655963.9c5818","type":"ui_text","z":"82480c90.eb63c","group":"85fb1c35.e8786","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":590,"y":120,"wires":},{"id":"8e484b23.a5dce8","type":"mongodb2 in","z":"82480c90.eb63c","service":"ext","configNode":"2666b88d.91e6f8","name":"","collection":"cihazlar","operation":"insert","x":310,"y":360,"wires":[["42221050.c7375"]]},{"id":"4e20cb46.5ae044","type":"function","z":"82480c90.eb63c","name":"MQTT to InfluxDB","func":"msg.payload = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":210,"y":300,"wires":[["8e484b23.a5dce8"]]},{"id":"42221050.c7375","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":410,"y":420,"wires":},{"id":"20b98e5f.a35d02","type":"mqtt-broker","z":"82480c90.eb63c","name":"centos","broker":"192.168.1.104","port":"1883","clientid":"node-red","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"bff5e425.ff6f68","type":"influxdb","z":"","hostname":"192.168.1.104","port":"8086","protocol":"http","database":"enriot","name":"","usetls":false,"tls":""},{"id":"85fb1c35.e8786","type":"ui_group","z":"","name":"Default","tab":"c07006d.07c3af8","disp":true,"width":"6","collapse":false},{"id":"2666b88d.91e6f8","type":"mongodb2","z":"","uri":"mongodb://localhost:27017/ENRIOT","name":"","options":"","parallelism":"-1"},{"id":"c07006d.07c3af8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

  1. Your flow cannot be imported, this is the same problem as last time, please reread the post on how to post a flow.

  2. What node are you using there are lots of mongodb nodes https://flows.nodered.org/?term=mongodb&type=node&num_pages=1

Assuming you are using node-red-mongodb the message you need to send to update is described in the ReadMe, so that would be a good place to start

You didn't understand me.
I want to update if there is a record in a nodename field.
like the following link

I think it is you that do not understand. You have not responded to questions 1 or 2 that @ukmoose asked.

[{"id":"82480c90.eb63c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"198a0d21.d320f3","type":"mqtt in","z":"82480c90.eb63c","name":"","topic":"/deneme/#","qos":"0","broker":"20b98e5f.a35d02","x":80,"y":100,"wires":[["e7872a27.108a38"]]},{"id":"ed7155f2.0b3398","type":"influxdb out","z":"82480c90.eb63c","influxdb":"bff5e425.ff6f68","name":"","measurement":"nodes","precision":"","retentionPolicy":"","x":680,"y":80,"wires":[]},{"id":"e7872a27.108a38","type":"json","z":"82480c90.eb63c","name":"","property":"payload","action":"","pretty":false,"x":210,"y":100,"wires":[["3f92a06b.8a235","4e20cb46.5ae044","f9f53243.c7b2e"]]},{"id":"3f92a06b.8a235","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":390,"y":100,"wires":[]},{"id":"f9f53243.c7b2e","type":"function","z":"82480c90.eb63c","name":"MQTT to InfluxDB","func":"\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":60,"wires":[["9b5c50ba.dbe6e","ed7155f2.0b3398","b9655963.9c5818"]]},{"id":"9b5c50ba.dbe6e","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":40,"wires":[]},{"id":"b9655963.9c5818","type":"ui_text","z":"82480c90.eb63c","group":"85fb1c35.e8786","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":590,"y":120,"wires":[]},{"id":"8e484b23.a5dce8","type":"mongodb2 in","z":"82480c90.eb63c","service":"ext","configNode":"2666b88d.91e6f8","name":"","collection":"cihazlar","operation":"insert","x":310,"y":360,"wires":[["42221050.c7375"]]},{"id":"4e20cb46.5ae044","type":"function","z":"82480c90.eb63c","name":"MQTT to InfluxDB","func":"msg.payload = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":210,"y":300,"wires":[["8e484b23.a5dce8"]]},{"id":"42221050.c7375","type":"debug","z":"82480c90.eb63c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":410,"y":420,"wires":[]},{"id":"20b98e5f.a35d02","type":"mqtt-broker","z":"82480c90.eb63c","name":"centos","broker":"192.168.1.104","port":"1883","clientid":"node-red","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"bff5e425.ff6f68","type":"influxdb","z":"","hostname":"192.168.1.104","port":"8086","protocol":"http","database":"enriot","name":"","usetls":false,"tls":""},{"id":"85fb1c35.e8786","type":"ui_group","z":"","name":"Default","tab":"c07006d.07c3af8","disp":true,"width":"6","collapse":false},{"id":"2666b88d.91e6f8","type":"mongodb2","z":"","uri":"mongodb://localhost:27017/ENRIOT","name":"","options":"","parallelism":"-1"},{"id":"c07006d.07c3af8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

  1. mongodb2

That is still not importable, for future posts how to share a flow is shown here: How to share code or flow json

The link you posted shows how to do it so I'm guessing you have done this now.

Yeah.
I can add data, but what should I do to update the same record

mqtt

I'm sending the following json data.
I sent the data "nodeName" is the same as the incoming json object how do I get updated according to json line.

Json: { "nodebolge": "Dnme","nodename": "nameAlani", "nodeabone": "abone2", "endeks": "655477" }

var newMsg = {
'collection' : 'cihazlar',
'operation': 'update',
'payload': {
'nodebolge': "ttttt",
'nodename': "nameAlani",
'nodeabone': "abone2",
'endeks': "endeks"
}
};
return newMsg;

The post that you added gives the answer of how to update. Just follow the solution that you provided.

I have tried to use the node mongodb2 and mongodb3 to try to update a record that is written, without succeeding. Could someone help me to review my structure and validate what may be happening?

The objective is to be able to add an object to a record that is already stored using the "_id" as a filter for the update.

[{"id":"8bf4e066.fb891","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"fdd747ef.bc9c38","type":"mongodb3 in","z":"8bf4e066.fb891","service":"_ext_","configNode":"5e1ddbe0.796184","name":"","collection":"","operation":"update","x":460,"y":140,"wires":[["3411da85.307a66"]]},{"id":"3411da85.307a66","type":"debug","z":"8bf4e066.fb891","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":260,"wires":[]},{"id":"9ddb94a8.11ba58","type":"inject","z":"8bf4e066.fb891","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload.query","v":"{\"_id\"=\"5f30320db06b690010296077\"}","vt":"json"},{"p":"payload"},{"p":"collection","v":"PLC_DRIYPAC_1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"status\":\"203\"","payloadType":"str","x":110,"y":400,"wires":[[]]},{"id":"9e5e4b61.f90098","type":"inject","z":"8bf4e066.fb891","name":"","props":[{"p":"collection","v":"PLC_DRIYPAC_1","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"$match\":{\"_id\":\"5f30320db06b690010296072\"},\"$set\":{\"sys.status\":\"203\"}}","payloadType":"json","x":270,"y":340,"wires":[["21f5de0.96e9f22"]]},{"id":"21f5de0.96e9f22","type":"mongodb in","z":"8bf4e066.fb891","mongodb":"cd1be4f1.fcbb98","name":"Data Collection","collection":"","operation":"aggregate","x":440,"y":340,"wires":[["3411da85.307a66"]]},{"id":"1c319556.f25b8b","type":"function","z":"8bf4e066.fb891","name":"insert (or delete)","func":"var newMsg = {};\nnewMsg.collection = 'PLC_DRIYPAC_1';\nnewMsg.payload    = {_id:(\"5f30320db06b690010296072\")},{$set:{\"syn\":{\"status\":\"203\"}}}\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":140,"wires":[["fdd747ef.bc9c38"]]},{"id":"4086cab3.bc2b64","type":"mongodb-node","z":"8bf4e066.fb891","mongodb":"a5ba9623.f01b88","name":"","collection":"","operation":"update","upsert":false,"multi":false,"x":200,"y":440,"wires":[[]]},{"id":"33bff2c3.3bc00e","type":"inject","z":"8bf4e066.fb891","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":80,"wires":[["1c319556.f25b8b"]]},{"id":"5e1ddbe0.796184","type":"mongodb3","z":"","uri":"mongodb://172.17.0.3:27017/miobox?authSource=admin","name":"miobox","options":"","parallelism":"-1"},{"id":"cd1be4f1.fcbb98","type":"mongodb","z":"","hostname":"172.17.0.3","port":"27017","db":"miobox?authSource=admin","name":""},{"id":"a5ba9623.f01b88","type":"mongodb-config","z":"","hostname":"172.17.0.3","port":"27017","db":"miobox?authSource=admin","name":""}]

Hi,

i dont know about mongodb2 or mongodb3
but I have been testing node-red-contrib-mongodb version 0.3.4

and you can use the mongo node operation set to "update" and use a function before it with example :

var newMsg = {};
newMsg.collection = 'PLC_DRIYPAC_1';
newMsg.query = {"_id": "5f3123aa60ac283b8a7e21f0"};
newMsg.payload = {"$set": {"status": "777"}};

return newMsg;

a test flow example :

[{"id":"1aa86ea6.d665a1","type":"debug","z":"58d20878.b6eef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":160,"wires":[]},{"id":"4207f55c.7ac6e4","type":"function","z":"58d20878.b6eef8","name":"insert","func":"var newMsg = {};\nnewMsg.collection = 'PLC_DRIYPAC_1';\nnewMsg.payload = {\"status\":\"203\"}\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":160,"wires":[["83ff43b1.59ece8"]]},{"id":"28f711dc.7848be","type":"inject","z":"58d20878.b6eef8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":80,"wires":[["4207f55c.7ac6e4"]]},{"id":"83ff43b1.59ece8","type":"mongodb-node","z":"58d20878.b6eef8","mongodb":"15e56f9b.d519c8","name":"","collection":"","operation":"insert","upsert":false,"multi":false,"x":620,"y":160,"wires":[["1aa86ea6.d665a1"]]},{"id":"74044eac.e0484","type":"debug","z":"58d20878.b6eef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":400,"wires":[]},{"id":"c782a26e.9ddde8","type":"function","z":"58d20878.b6eef8","name":"find","func":"var newMsg = {};\nnewMsg.collection = 'PLC_DRIYPAC_1';\nnewMsg.payload = {};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":400,"wires":[["6bccaad7.d79244"]]},{"id":"eda26919.0cd938","type":"inject","z":"58d20878.b6eef8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":320,"wires":[["c782a26e.9ddde8"]]},{"id":"6bccaad7.d79244","type":"mongodb-node","z":"58d20878.b6eef8","mongodb":"15e56f9b.d519c8","name":"","collection":"","operation":"find","upsert":false,"multi":false,"x":680,"y":400,"wires":[["74044eac.e0484"]]},{"id":"4c8a24c8.246d44","type":"debug","z":"58d20878.b6eef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":620,"wires":[]},{"id":"df180e49.f0c2e","type":"function","z":"58d20878.b6eef8","name":"update","func":"//const { ObjectId } = global.get('mongodb');\n\nvar newMsg = {};\nnewMsg.collection = 'PLC_DRIYPAC_1';\nnewMsg.query = {\"_id\": \"5f312ae860ac283b8a7e21f1\"};\nnewMsg.payload = {\"$set\": {\"status\": \"777\"}};\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":600,"wires":[["76a1ac70.b7f2c4"]]},{"id":"7221dfd6.d3ef7","type":"inject","z":"58d20878.b6eef8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":540,"wires":[["df180e49.f0c2e"]]},{"id":"76a1ac70.b7f2c4","type":"mongodb-node","z":"58d20878.b6eef8","mongodb":"15e56f9b.d519c8","name":"","collection":"","operation":"update","upsert":false,"multi":false,"x":680,"y":620,"wires":[["4c8a24c8.246d44"]]},{"id":"15e56f9b.d519c8","type":"mongodb-config","z":"","hostname":"127.0.0.1","port":"27017","name":""}]
1 Like

Great!

It is already working without problems, I am going to make some changes to adapt the data but I could say that it will work.

I always got a response that replaced all the stored data.

Hello - your message helped me a lot! For one record this works like a charm! But how the newMsg.query would look like if I want to update many IDs with the same message.paylod? After like 6 hours I am not still able to find any solution.

My query is like this:

{
    "_id": {
        "$in": [
            "6242170245341c11dc2133da",
            "6242173e45341c11dc2133dc"
        ]
    }
}

A tried many variations and I am super lost now...

I welcome any advice :slight_smile:

Hello .. i tried the $in array of _id strings syntax also without success ..

what worked for me in the end is to use the special ObjectId around each id
but in order to use that you must first load the mongodb library,
using Node-red's functionExternalModules option (link)

image

Then in the Function

let ObjectId = mongodb.ObjectId

let newMsg = {};

newMsg.query = { "_id": { "$in": [ObjectId("60000f1879d9790bac657a94"), ObjectId("6000162079d9790bac657a95")] } };

newMsg.payload = { "$set": { "testField": 888 } };
    
return newMsg;

Dear Unborn - arn't you from the Czech Republic? U sir, u deserve a beer or huge awesome coffee.... I spent a huge amount of time on this...

THANK YOU! :slight_smile:

Jaroslav

1 Like