8

很简单,使用 mongo cli:

db.version ()

我怎样才能对猫鼬做同样的事情?如何发送自定义命令?

4

3 回答 3

14

您可以通过 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);
  });
});
于 2013-03-09T14:36:16.427 回答
2

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

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

于 2019-01-04T02:54:35.067 回答
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);
    });
于 2020-11-20T00:44:51.253 回答