如何从mongoose获取mongodb版本

时间:2013-03-09 13:38:25

标签: node.js mongodb mongoose

简单,使用mongo cli:

db.version ()

我如何用猫鼬做同样的事情?如何发送自定义命令?

3 个答案:

答案 0 :(得分:8)

您可以通过Mongoose连接使用本机mongo驱动程序的Admin#buildInfo方法:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', function(err){
  var admin = new mongoose.mongo.Admin(mongoose.connection.db);
  admin.buildInfo(function (err, info) {
     console.log(info.version);
  });
});

答案 1 :(得分:1)

您可以直接从Mongoose连接中查询buildInfo

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', function(err) {
    mongoose.db.command({ buildInfo: 1 }, function (err, info) {
        console.log(info.version);
    });
});

https://docs.mongodb.com/manual/reference/command/buildInfo/#dbcmd.buildInfo

答案 2 :(得分:0)

尝试一下,它将为您提供MongoDB和Mongoose的版本

async function run() {
    var admin = new mongoose.mongo.Admin(mongoose.connection.db);
    admin.buildInfo(function (err, info) {
       console.log(`mongodb: ${info.version}`);
       console.log(`mongoose: ${mongoose.version}`);
    });
}

mongoose.connect(process.env.MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => {
        console.log('MongoDB connected');
        run();
    })
    .catch(error => {
        console.log(error);
    });