Reputation: 1307
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()
Upvotes: 6
Views: 6831
Reputation: 11
This is what I did:
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase db = mongoClient.getDatabase("test");
Document document = db.runCommand(new Document("buildInfo",1));
System.out.println("MongoDB Version: "+document.getString("version"));
My Configs
- MongoDB JVM Driver: 4.1
- JDK 11
Upvotes: 1
Reputation: 105083
This one works for me (Java client 3.5.0):
MongoClient client = //..
String version = client.getDatabase("dbname")
.runCommand(new BsonDocument("buildinfo", new BsonString("")))
.get("version")
.toString();
Upvotes: 3
Reputation: 1307
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
Upvotes: 7
Reputation: 230366
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).
Upvotes: 6