top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the connected hostname for mongo instance using node.js?

0 votes
272 views

I have 3 node replicates and i am using all the host in my connection string.Once i execute my node.js script i want to see which particular host it got connected to.
I can fire the following command in mongo shell but same is not working in node.js script, mostly says like "function is not defined"

hostnamedb.hostInfo().system.hostnamers.status().members.find(r=>r.state===1).name
TypeError: db.hostInfo is not a function
ReferenceError: hostname is not defined

Can someone help me how i can find the connected mongo instance server name using the node.js script?

posted Oct 24, 2016 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+1 vote

I am new to Node and Mongo, I was trying to connect to a mongo database in one file and export the database handler to many other files, so that I need not to connect to the database in all files which need a connection to it. Here is how I attempted to do it

// db.js
var client = require(mongodb).MongoClient
var assert = require(assert)
var url = mongodb://localhost:27017/test

client.connect(url, (err, db) => {
  assert.equal(err, null)
  module.exports = db
})

After exporting the db handler, I tried to access methods on it in another file as follows

var db = require(./db)
console.log(db.collection(col))

but it throws a TypeError, saying that db.collection is not a function. How can I access the methods on db handler in other files?

0 votes

is it possible to create views using the Node.js MongoDB Driver (3.0)? There is no db.createView() in the drivers API reference.

+1 vote

I am using mongo db version 2.2.3 . I am having the requirement like user gives the input like "(ash|ben) & (Bus|car)" as search keywords. In this the & is for AND condition and | for the OR condition.
Is there any way to make this nested leywords as a mongodb query in node js. Is there any functionality in mongodb to achieve this?. If not how can make this in any other way?

0 votes

If you wanted to copy over the mongo database using mongos oplog, how would you do it? Suppose we start copying the database at time T and we finish the copying at time U. We have to read from the oplog from time T onward and apply oplog changes (which also may have been applied between time T and U). Do the oplog transactions have timestamps in them so we can discard records before time T? Are the operations idempotent if we apply them twice (because we are applying an operation already executed between time T and U)? Guess I am confused how to get the initial data before reading from the oplog to continue the real time updates.

...