how to execute mongo admin command from java

后端 未结 3 1128
醉话见心
醉话见心 2020-12-16 22:41

I want to execute soem admin command with parameters from java.

The commands are:

{ enablesharding : \"test\" }
{ shardcollection : \"test.test_coll         


        
相关标签:
3条回答
  • 2020-12-16 23:08

    Have you ensured you have authenticated to the db successfully?

    Have you tried db.eval(COMMAND_THAT_YOU_WANT_TO_EVAL);

    0 讨论(0)
  • 2020-12-16 23:14

    I just want to add that Julias's answer is correct, but now it's deprecated. You could use new API (Document class is from package org.bson):

    MongoDatabase database = client.getDatabase("admin");
    Document documentA = database.runCommand(new Document("enablesharding", "test"));
    Document documentB = database.runCommand(
            new Document("shardcollection", "testDB.x").append("key", new Document("userId", 1)));
    
    0 讨论(0)
  • 2020-12-16 23:18

    I just found it

    DB db = mongo.getDB("admin");
    DBObject cmd = new BasicDBObject();
    cmd.put("shardcollection", "testDB.x");
    cmd.put("key", new BasicDBObject("userId", 1));
    CommandResult result = db.command(cmd);
    
    0 讨论(0)
提交回复
热议问题