Bob*_*b B 2 mongodb mongodb-java
从MongoDB Java驱动程序版本2.10.1设置分片键的语法是什么?
换句话说,我如何使用Java驱动程序执行此操作?
sh.shardCollection("test.a", {"_id": "hashed"}})
Run Code Online (Sandbox Code Playgroud)
简短的回答:你应该发出一个shardCollection命令.
答案很长:
在sh.shardCollectionMongoDB中壳只是调用的命令一个辅助方法admin分贝.
如果您输入sh.shardCollectionMongoDB shell,您将看到此函数实际执行的操作:
> sh.shardCollection
function ( fullName , key , unique ) {
sh._checkFullName( fullName )
assert( key , "need a key" )
assert( typeof( key ) == "object" , "key needs to be an object" )
var cmd = { shardCollection : fullName , key : key }
if ( unique )
cmd.unique = true;
return sh._adminCommand( cmd );
}
Run Code Online (Sandbox Code Playgroud)
然后sh._adminCommand,您可以调用MongoDB shell:
> sh._adminCommand
function ( cmd , skipCheck ) {
if ( ! skipCheck ) sh._checkMongos();
return db.getSisterDB( "admin" ).runCommand( cmd );
}
Run Code Online (Sandbox Code Playgroud)
将所有内容放在一起时,所有sh.shardCollection命令都在检查参数并调用此命令:
db.getSisterDB( "admin" ).runCommand({
shardCollection : "test.a" ,
key : {"_id": "hashed"}
});
Run Code Online (Sandbox Code Playgroud)
Java语法:
DBObject cmd = new BasicDBObject("shardCollection", "test.a").
append("key",new BasicDBObject("_id", "hashed"));
CommandResult r = db.getSisterDB("admin").command(cmd);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2516 次 |
| 最近记录: |