node-red-contrib-mongodb2 example 2

Here are a few examples of how to use the following MongoDB commands with Node Red

  • insert (Use this to create mongodb documents)
  • findOne
  • find.forEach
  • find.toArray

To use these flows, you need to

  • Install mongodb to your PC and have it running
  • Install the mongodb2 node to node-red

Hopefully the functions make sense about what they do. If not just output to the debug panel to see what's happening.
A large part of MongoDB involves the understanding of the following syntax of a mongodb document { {"name":"John", "surname":"Wayne"} , {"name":"Charlie", "surname":"Chaplin"} ]

[{"id":"44fc0d6f.e1e8f4","type":"mongodb2","hostname":"127.0.0.1","port":"27017","db":"test","name":""},{"id":"641f9718.3bc628","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":666,"y":331,"z":"a984f74a.76cb2","wires":[]},{"id":"d066e5b1.29b4c","type":"inject","name":"2) Output Separately for each Record","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":168,"y":234,"z":"a984f74a.76cb2","wires":[["cb0238cd.f636d8"]]},{"id":"c7d346c2.e9657","type":"mongodb2 in","service":"_ext_","configNode":"44fc0d6f.e1e8f4","name":"Mongo DB dynamic operation","collection":"","operation":"","x":326,"y":305,"z":"a984f74a.76cb2","wires":[["8037a8d5.9568c"]]},{"id":"e3cd50e9.1192e","type":"mongodb2 in","service":"_ext_","configNode":"44fc0d6f.e1e8f4","name":"Mongo DB dynamic operation","collection":"names","operation":"","x":336,"y":92,"z":"a984f74a.76cb2","wires":[["8037a8d5.9568c"]]},{"id":"2150137d.f8fe24","type":"inject","name":"1) Find One only","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":102,"y":32,"z":"a984f74a.76cb2","wires":[["5e1f6911.ea48d"]]},{"id":"5e1f6911.ea48d","type":"function","name":"findOne","func":" var newMsg = {};\nnewMsg.operation  = 'findOne';\nnewMsg.payload    = { 'name' : 'Pete1' , 'names' : 'Pete2' };\nnewMsg.projection = { 'name' : 1 , '_id' : 0 };\nreturn newMsg;","outputs":1,"valid":true,"x":128,"y":91,"z":"a984f74a.76cb2","wires":[["e3cd50e9.1192e"]]},{"id":"d5a57a38.347fd8","type":"mongodb2 in","service":"_ext_","configNode":"44fc0d6f.e1e8f4","name":"Store Data into MongoDB","collection":"","operation":"","x":459,"y":383,"z":"a984f74a.76cb2","wires":[[]]},{"id":"cb0238cd.f636d8","type":"function","name":"find.forEach","func":"var newMsg = {};\nnewMsg.collection = 'names';\nnewMsg.operation  = 'find.forEach';\n//newMsg.payload    = { 'name' : 'Pete1' , 'names' : 'Pete2' };\nnewMsg.payload    = { 'name' : 'Pete1' };\nnewMsg.projection = { 'name' : 1 , '_id' : 0 };\nreturn newMsg;","outputs":1,"valid":true,"x":108,"y":305,"z":"a984f74a.76cb2","wires":[["c7d346c2.e9657"]]},{"id":"8037a8d5.9568c","type":"function","name":"Just get name","func":"var newMsg = {};\nnewMsg.payload    = msg.payload.name;\nreturn newMsg;","outputs":1,"valid":true,"x":548,"y":152,"z":"a984f74a.76cb2","wires":[["641f9718.3bc628"]]},{"id":"b21872e.7f75c9","type":"inject","name":"4) Output once only as a list","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":140,"y":473,"z":"a984f74a.76cb2","wires":[["5f57a322.8a5ed4"]]},{"id":"5f57a322.8a5ed4","type":"function","name":"find.toArray","func":"var newMsg = {};\nnewMsg.collection = 'names';\nnewMsg.operation  = 'find.toArray';\n//newMsg.payload    = { 'name' : 'Pete1' , 'names' : 'Pete2' };\nnewMsg.payload    = { 'name' : 'Pete1' };\nnewMsg.projection = { 'name' : 1 , '_id' : 0 };\nreturn newMsg;","outputs":1,"valid":true,"x":104,"y":532,"z":"a984f74a.76cb2","wires":[["ec979d20.9e7328"]]},{"id":"ec979d20.9e7328","type":"mongodb2 in","service":"_ext_","configNode":"44fc0d6f.e1e8f4","name":"Mongo DB dynamic operation","collection":"","operation":"","x":315,"y":532,"z":"a984f74a.76cb2","wires":[["91a4ffff.acc4a"]]},{"id":"91a4ffff.acc4a","type":"function","name":"get names as a list","func":"// Outputs separate messages !!\n//var newMsg =[];\n//for (var i = 0; i < msg.payload.length; i++) { \n//newMsg.push({name: msg.payload[i].name}) };\n//return [ newMsg ];\n\n// Combines into a single list (or array)\nvar newMsg =[];\nfor (var i = 0; i < msg.payload.length; i++) {\n//newMsg.push({name:  msg.payload[i].name } ) };\nnewMsg.push(msg.payload[i].name) };\nmsg.payload = newMsg;\nreturn  msg;\n","outputs":1,"valid":true,"x":549,"y":532,"z":"a984f74a.76cb2","wires":[["641f9718.3bc628"]]},{"id":"b4995ada.eeba08","type":"inject","name":"3) Enter data","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":89,"y":384,"z":"a984f74a.76cb2","wires":[["f1950f87.ec4da"]]},{"id":"f1950f87.ec4da","type":"function","name":"insert (or delete)","func":"var newMsg = {};\nnewMsg.collection = 'names';\n//newMsg.operation  = 'deleteOne';\nnewMsg.operation  = 'insert';\nnewMsg.payload    = { 'name' : 'Pete1' , 'names' : 'Smith4' };\nreturn newMsg;","outputs":1,"valid":true,"x":250,"y":384,"z":"a984f74a.76cb2","wires":[["d5a57a38.347fd8"]]}]
CurlyWurly-1

Flow Info

created 2 years, 2 months ago

Node Types

Core
  • debug (x1)
  • function (x6)
  • inject (x4)
Other

Tags

  • mongodb
  • mongodb2
  • find
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option