From d728b99de966bbd929b0cd763df254bcff948ed7 Mon Sep 17 00:00:00 2001 From: Ross Cruickshank Date: Tue, 8 Sep 2020 10:46:02 +0100 Subject: [PATCH] useV3 connections and support clusters use v3.6.1+ mongodb client -- need to extract db() from connection client before accessing collections allow specifying 3.4+ type connection to sharded clusters (mongodb+srv://clustername ) in addition to "old style" (mongodb://host:port) --- storage/mongodb/66-mongodb.js | 46 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/storage/mongodb/66-mongodb.js b/storage/mongodb/66-mongodb.js index a8f66f35..14fafe82 100644 --- a/storage/mongodb/66-mongodb.js +++ b/storage/mongodb/66-mongodb.js @@ -1,4 +1,5 @@ + module.exports = function(RED) { "use strict"; var mongo = require('mongodb'); @@ -11,13 +12,37 @@ module.exports = function(RED) { this.port = n.port; this.db = n.db; this.name = n.name; + this.clustered = n.clustered; - var url = "mongodb://"; - if (this.credentials && this.credentials.user && this.credentials.password) { - url += this.credentials.user+":"+this.credentials.password+"@"; + // if(n.user && n.password){ + // this.credentials.user = n.user; + // this.credentials.password= n.password; + // } + + //console.log(this); + //console.log("\n\n\n\n\n",n); + + var url = "mongodb+srv://"; + if (!this.clustered) { + url = "mongodb://"; } - url += this.hostname+":"+this.port+"/"+this.db; + if (this.credentials && this.credentials.user && this.credentials.password) { + this.user = this.credentials.user; + this.password = this.credentials.password; + } else { + this.user = n.user; + this.password = n.password; + } + if (this.user) { + url += this.user+":"+this.password+"@"; + } + if (this.clustered) { + url += this.hostname + "/" + this.db + "?retryWrites=true&w=majority"; + } else { + url += this.hostname + ":" + this.port + "/" + this.db; + }; + console.log(url); this.url = url; } @@ -49,7 +74,7 @@ module.exports = function(RED) { var noerror = true; var connectToDB = function() { - MongoClient.connect(node.mongoConfig.url, function(err, db) { + MongoClient.connect(node.mongoConfig.url, function(err, client) { if (err) { node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); if (noerror) { node.error(err); } @@ -58,9 +83,12 @@ module.exports = function(RED) { } else { node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); - node.clientDb = db; + node.clientDb = client.db(); + var db = client.db(); + console.log( db); noerror = true; var coll; + if (node.collection) { coll = db.collection(node.collection); } @@ -174,7 +202,8 @@ module.exports = function(RED) { var noerror = true; var connectToDB = function() { - MongoClient.connect(node.mongoConfig.url, function(err,db) { + console.log("connecting:" + node.mongoConfig.url); + MongoClient.connect(node.mongoConfig.url, function(err,client) { if (err) { node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); if (noerror) { node.error(err); } @@ -183,7 +212,8 @@ module.exports = function(RED) { } else { node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); - node.clientDb = db; + node.clientDb = client.db(); + var db = client.db(); noerror = true; var coll; node.on("input", function(msg) {