问题
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!
回答1:
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);
})
来源:https://stackoverflow.com/questions/63039377/how-to-run-mongo-shell-commands-in-node-js-mongoose