From 23b5ac45825213016e882fd002774a354f542e82 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Fri, 19 Sep 2014 10:06:15 +0100 Subject: [PATCH] Allow user to select collection in mongo nodes using msg.collection --- nodes/core/storage/66-mongodb.html | 74 +++++++++++++++---------- nodes/core/storage/66-mongodb.js | 89 +++++++++++++++++++++--------- 2 files changed, 109 insertions(+), 54 deletions(-) diff --git a/nodes/core/storage/66-mongodb.html b/nodes/core/storage/66-mongodb.html index 246b9bac8..81c563897 100644 --- a/nodes/core/storage/66-mongodb.html +++ b/nodes/core/storage/66-mongodb.html @@ -40,21 +40,21 @@ @@ -72,7 +72,7 @@
+ diff --git a/nodes/core/storage/66-mongodb.js b/nodes/core/storage/66-mongodb.js index c84358e3f..7a990f19d 100644 --- a/nodes/core/storage/66-mongodb.js +++ b/nodes/core/storage/66-mongodb.js @@ -54,32 +54,64 @@ module.exports = function(RED) { if (this.mongoConfig) { var node = this; - MongoClient.connect(this.mongoConfig.url, function(err,db) { + MongoClient.connect(this.mongoConfig.url, function(err, db) { if (err) { node.error(err); } else { node.clientDb = db; - var coll = db.collection(node.collection); + var coll; + if (node.collection) { + coll = db.collection(node.collection); + } node.on("input",function(msg) { - if (node.operation === "save") { - delete msg._topic; - if (node.payonly) { - if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; } - coll.save(msg.payload,function(err,item){ if (err){node.error(err);} }); + if (!coll) { + if (msg.collection) { + coll = db.collection(msg.collection); } else { - coll.save(msg,function(err,item){if (err){node.error(err);}}); + node.error("No collection defined"); + return; } } - else if (node.operation === "insert") { - delete msg._topic; + delete msg._topic; + delete msg.collection; + if (node.operation === "store") { if (node.payonly) { - if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; } - coll.insert(msg.payload,function(err,item){ if (err){node.error(err);} }); + if (typeof msg.payload !== "object") { + msg.payload = {"payload": msg.payload}; + } + coll.save(msg.payload,function(err, item) { + if (err) { + node.error(err); + } + }); } else { - coll.insert(msg,function(err,item){if (err){node.error(err);}}); + coll.save(msg,function(err, item) { + if (err) { + node.error(err); + } + }); + } + } else if (node.operation === "insert") { + if (node.payonly) { + if (typeof msg.payload !== "object") { + msg.payload = {"payload": msg.payload}; + } + coll.insert(msg.payload, function(err, item) { + if (err) { + node.error(err); + } + }); + } else { + coll.insert(msg, function(err,item) { + if (err) { + node.error(err); + } + }); + } + } else if (node.operation === "update") { + if (typeof msg.payload !== "object") { + msg.payload = {"payload": msg.payload}; } - } - else if (node.operation === "update") { var query = msg.query || {}; var payload = msg.payload || {}; var options = { @@ -89,11 +121,10 @@ module.exports = function(RED) { coll.update(query, payload, options, function(err, item) { if (err) { - node.error(err); + node.error(err + " " + payload); } }); - } - if (node.operation == "delete") { + } else if (node.operation === "delete") { coll.remove(msg.payload, function(err, items) { if (err) { node.error(err); @@ -115,7 +146,6 @@ module.exports = function(RED) { } RED.nodes.registerType("mongodb out",MongoOutNode); - function MongoInNode(n) { RED.nodes.createNode(this,n); this.collection = n.collection; @@ -130,8 +160,19 @@ module.exports = function(RED) { node.error(err); } else { node.clientDb = db; - var coll = db.collection(node.collection); - node.on("input",function(msg) { + var coll; + if (node.collection) { + coll = db.collection(node.collection); + } + node.on("input", function(msg) { + if (!coll) { + if (msg.collection) { + coll = db.collection(msg.collection); + } else { + node.error("No collection defined"); + return; + } + } if (node.operation === "find") { msg.projection = msg.projection || {}; coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) { @@ -145,8 +186,7 @@ module.exports = function(RED) { node.send(msg); } }); - } - else if (node.operation === "count") { + } else if (node.operation === "count") { coll.count(msg.payload, function(err, count) { if (err) { node.error(err); @@ -155,8 +195,7 @@ module.exports = function(RED) { node.send(msg); } }); - } - else if (node.operation === "aggregate") { + } else if (node.operation === "aggregate") { msg.payload = (msg.payload instanceof Array) ? msg.payload : []; coll.aggregate(msg.payload, function(err, result) { if (err) {