很简单,使用 mongo cli:
db.version ()
我怎样才能对猫鼬做同样的事情?如何发送自定义命令?
您可以通过 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);
});
});
您可以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
试试这个,它会给你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);
});