i see .. thats ok .. we'll have another go with the Mongodb "update" command.
The example below uses nodered
db and test
collection.
On first entry it adds a document and what is unique of this document, except from _id that is added by mongo, is the device_id: "A8175804FCE5"
.. originally named _id
in your flow but i renamed it to device_id
because it makes things easier to update.
On subsequent msgs we select the document based on device_id
and $set (update) existing fields and add new ones if they are missing.
Example Flow :
[{"id":"66bf3280.eeb04c","type":"inject","z":"54efb553244c241f","name":"Data Set 1","props":[{"p":"payload.mmi_DevEUI","v":"A81758FFFE04FCE5","vt":"str"},{"p":"payload.ts","v":"","vt":"date"},{"p":"payload.item1","v":"blabla","vt":"str"},{"p":"payload.item2","v":"12345","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":2080,"wires":[["53da6288.39be74"]]},{"id":"481f2246.b8667c","type":"inject","z":"54efb553244c241f","name":"Data Set 2","props":[{"p":"payload.mmi_DevEUI","v":"A81758FFFE04FCE5","vt":"str"},{"p":"payload.ts","v":"","vt":"date"},{"p":"payload.item3","v":"testtest","vt":"str"},{"p":"payload.item4","v":"67890","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":2120,"wires":[["53da6288.39be74"]]},{"id":"53da6288.39be74","type":"function","z":"54efb553244c241f","name":"shorten deveui to 12 char","func":"var str = msg.payload.mmi_DevEUI;\n\nvar str_short_begin = str.slice(0, 6);\nvar str_short_end = str.slice(10, 16);\nvar str_short = str_short_begin + str_short_end;\n\nmsg.payload.device_id = str_short;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":2100,"wires":[["fd507e0a.82d6a8","53472c44.bb002c"]]},{"id":"fd507e0a.82d6a8","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":2040,"wires":[]},{"id":"d503d41c.0cb168","type":"mongodb out","z":"54efb553244c241f","mongodb":"d5beac98.b8fba8","name":"mmiDevice ","collection":"test","payonly":true,"upsert":true,"multi":false,"operation":"update","x":850,"y":2100,"wires":[]},{"id":"53472c44.bb002c","type":"function","z":"54efb553244c241f","name":"update","func":"msg.query = { \"device_id\": msg.payload.device_id };\nmsg.payload = { $set: msg.payload }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":2100,"wires":[["d503d41c.0cb168"]]},{"id":"d5beac98.b8fba8","type":"mongodb","hostname":"localhost","topology":"direct","connectOptions":"useUnifiedTopology=true","port":"27017","db":"nodered","name":"","credentials":{}}]