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

╄→гoц情女王★ 提交于 2021-01-29 15:06:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!