user13880264
user13880264

Reputation:

How to run mongo shell commands in Node.js Mongoose?

I would like to run the isMaster() command within my node.js project. The problem is that I'm unsure how to run any sort of mongo shell command via js code.


I know in Python you can use client.admin.command('ismaster').


If this isn't achievable within mongoose, I am open to using the mongodb package, but I'd like to keep it solely mongoose. I am just trying to test if a connection is within a primary node.

Thanks!

Upvotes: 2

Views: 1879

Answers (1)

Tunmise Ogunniyi
Tunmise Ogunniyi

Reputation: 2573

You can use either Db.executeDbAdminCommand or Db.admin().command.

This is how you would run it with mongoose:

mongoose.connection.db.admin().command({ isMaster: 1 }, (err, result) => {
  console.log('Result: ', result);
})

or

mongoose.connection.executeDbAdminCommand({ isMaster: 1 }, (err, result) => {
  console.log('Result: ', result);
})

Upvotes: 1

Related Questions