正如标题所示,我想获得客户端连接的mongo实例的版本。目前我使用的是mongo java驱动程序2.9.3,mongo实例是2.2.2。
我需要这样做,以便同时支持$pushAll
和$push
$each
个函数,因为从版本2.4中弃用了旧版本而不支持后者。总之,我想知道等同于db.version()
答案 0 :(得分:7)
在未来版本的驱动程序提供方法之前,由于来自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
答案 1 :(得分:5)
> 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
}
所以你可以在你的java代码中使用等效的runCommand
(不知道java驱动程序,我是ruby家伙)。
答案 2 :(得分:1)
这个适用于我(Java客户端3.5.0):
MongoClient client = //..
String version = client.getDatabase("dbname")
.runCommand(new BsonDocument("buildinfo", new BsonString("")))
.get("version")
.toString();
答案 3 :(得分:0)
这就是我所做的:
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"));
我的配置
- MongoDB JVM驱动程序:4.1
- JDK 11