y.vachnish
y.vachnish

Reputation: 5

Insert data using Node-red to external mongoDB

I'm new at Node-Red & Javascript.

I'm trying to insert some "key:value" to external MongoDB (not the MongoDB inside the Node-red but rather external one using mlab.com service).

I've got from mlab (who holds the DB for me) link to insert some data to my db. I looked for "how to insert data to mlab using JS" and this what I found:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://<dbuser>:<dbpassword>@*******.mlab.com:****/*****";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myobj = { name: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});

The main problem is (according to what I have understood) Node-red isn't use "require()" (some import of JS) but some of global function that replace the "require()" part. If I'm trying to use "require()" the error:"ReferenceError: require is not defined (line 1, col 19)" appears. I have no idea how to implements the globalFunction and replace the "require()" job.

My question is: how I'm sending data using this link that I've received from mlab.com without using "require()" from Node-Red?

Upvotes: 0

Views: 1416

Answers (1)

hardillb
hardillb

Reputation: 59608

Do not try and use a function node to update the MongoDB, that would defeat the point of using Node-RED in the first place.

There are several MongoDB nodes listed on flows.nodered.org that will handle everything for you.

Upvotes: 1

Related Questions