2 * Copyright 2013,2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var mongo = require('mongodb');
20 var MongoClient = mongo.MongoClient;
22 function MongoNode(n) {
23 RED.nodes.createNode(this,n);
24 this.hostname = n.hostname;
29 var url = "mongodb://";
30 if (this.credentials && this.credentials.user && this.credentials.password) {
31 url += this.credentials.user+":"+this.credentials.password+"@";
33 url += this.hostname+":"+this.port+"/"+this.db;
38 RED.nodes.registerType("mongodb",MongoNode,{
41 password: {type: "password"}
45 function ensureValidSelectorObject(selector) {
46 if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
53 function MongoOutNode(n) {
54 RED.nodes.createNode(this,n);
55 this.collection = n.collection;
56 this.mongodb = n.mongodb;
57 this.payonly = n.payonly || false;
58 this.upsert = n.upsert || false;
59 this.multi = n.multi || false;
60 this.operation = n.operation;
61 this.mongoConfig = RED.nodes.getNode(this.mongodb);
63 if (this.mongoConfig) {
65 MongoClient.connect(this.mongoConfig.url, function(err, db) {
71 if (node.collection) {
72 coll = db.collection(node.collection);
74 node.on("input",function(msg) {
77 coll = db.collection(msg.collection);
79 node.error("No collection defined");
84 delete msg.collection;
85 if (node.operation === "store") {
87 if (typeof msg.payload !== "object") {
88 msg.payload = {"payload": msg.payload};
90 coll.save(msg.payload,function(err, item) {
96 coll.save(msg,function(err, item) {
102 } else if (node.operation === "insert") {
104 if (typeof msg.payload !== "object") {
105 msg.payload = {"payload": msg.payload};
107 coll.insert(msg.payload, function(err, item) {
113 coll.insert(msg, function(err,item) {
119 } else if (node.operation === "update") {
120 if (typeof msg.payload !== "object") {
121 msg.payload = {"payload": msg.payload};
123 var query = msg.query || {};
124 var payload = msg.payload || {};
130 coll.update(query, payload, options, function(err, item) {
132 node.error(err + " " + payload);
135 } else if (node.operation === "delete") {
136 coll.remove(msg.payload, function(err, items) {
146 this.error("missing mongodb configuration");
149 this.on("close", function() {
151 this.clientDb.close();
155 RED.nodes.registerType("mongodb out",MongoOutNode);
157 function MongoInNode(n) {
158 RED.nodes.createNode(this,n);
159 this.collection = n.collection;
160 this.mongodb = n.mongodb;
161 this.operation = n.operation || "find";
162 this.mongoConfig = RED.nodes.getNode(this.mongodb);
164 if (this.mongoConfig) {
166 MongoClient.connect(this.mongoConfig.url, function(err,db) {
172 if (node.collection) {
173 coll = db.collection(node.collection);
175 node.on("input", function(msg) {
177 if (msg.collection) {
178 coll = db.collection(msg.collection);
180 node.error("No collection defined");
184 if (node.operation === "find") {
185 msg.projection = msg.projection || {};
186 var selector = ensureValidSelectorObject(msg.payload);
187 coll.find(selector,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
192 delete msg.projection;
198 } else if (node.operation === "count") {
199 var selector = ensureValidSelectorObject(msg.payload);
200 coll.count(selector, function(err, count) {
208 } else if (node.operation === "aggregate") {
209 msg.payload = (msg.payload instanceof Array) ? msg.payload : [];
210 coll.aggregate(msg.payload, function(err, result) {
214 msg.payload = result;
223 this.error("missing mongodb configuration");
226 this.on("close", function() {
228 this.clientDb.close();
232 RED.nodes.registerType("mongodb in",MongoInNode);