Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting version of Mongo Instance with Java driver

Tags:

java

mongodb

As the title suggests, i want to get the version of the mongo instance the client is connecting to. Currently i am using mongo java driver 2.9.3 and mongo instance is 2.2.2.

I require this, in order to support both $pushAll and $push with $each functions, since former is deprecated from version 2.4 in favor of latter. In short I want to know Java driver equivalent of db.version()

like image 822
fgakk Avatar asked Oct 09 '13 14:10

fgakk


2 Answers

Until future versions of driver presents a method, current solution is following, thanks to hint from here.

DB db = new Mongo("127.0.0.1").getDB("test");//Better use MongoClient since Mongo class is deprecated
System.out.println(db.getMongo().getVersion());//prints 2.9.3 driverversion
CommandResult commandResult = db.command("buildInfo");
System.out.println(commandResult.getString("version"));//prints 2.4.2 Note tried at home since my mongo version is 2.4.2
like image 170
fgakk Avatar answered Oct 07 '22 06:10

fgakk


A little poking around revealed this:

> db.version()
2.4.6
> db.version
function (){
    return this.serverBuildInfo().version;
}
> db.serverBuildInfo
function (){
    return this._adminCommand( "buildinfo" );
}
> db.runCommand('buildinfo')
{
    "version" : "2.4.6",
    "gitVersion" : "b9925db5eac369d77a3a5f5d98a145eaaacd9673",
    "sysInfo" : "Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49",
    "loaderFlags" : "-fPIC -pthread -rdynamic",
    "compilerFlags" : "-Wnon-virtual-dtor -Woverloaded-virtual -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -pipe -fno-builtin-memcmp -O3",
    "allocator" : "tcmalloc",
    "versionArray" : [
        2,
        4,
        6,
        0
    ],
    "javascriptEngine" : "V8",
    "bits" : 64,
    "debug" : false,
    "maxBsonObjectSize" : 16777216,
    "ok" : 1
}

So you just can use equivalent of runCommand in your java code (don't know java driver, I'm ruby guy).

like image 23
Sergio Tulentsev Avatar answered Oct 07 '22 06:10

Sergio Tulentsev