Gabriel Llamas
Gabriel Llamas

Reputation: 18427

How to get mongodb version from mongoose

Simple, with the mongo cli:

db.version ()

How can I do the same with mongoose? How can I send a custom command?

Upvotes: 10

Views: 11729

Answers (3)

Maher Aldous
Maher Aldous

Reputation: 942

Try this one, it will give you the verion of both MongoDB and 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);
    });

Upvotes: 2

MernXL
MernXL

Reputation: 316

You can query the buildInfo directly from your Mongoose connection.

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

Upvotes: 5

JohnnyHK
JohnnyHK

Reputation: 311875

You can use the native mongo driver's Admin#buildInfo method for that via your Mongoose connection:

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);
  });
});

Upvotes: 17

Related Questions