How to execute mongo admin command from java

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

Teams:

{ enablesharding : "test" } { shardcollection : "test.test_collection", key : {"number":1} } 

How can I do this from a java driver?

The following code does not work:

 mongo.getDb("admin").command("{shardcollection : \"test.test_collection\", key:\"number\":1} }") 
+6
source share
3 answers

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); 
+14
source

I just want to add that Julias answer is correct, but now it is out of date. You can use the new API ( Document class from org.bson package):

 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
source

Did you guarantee that you have successfully authenticated in db?

Have you tried db.eval (COMMAND_THAT_YOU_WANT_TO_EVAL);

-1
source

Source: https://habr.com/ru/post/914608/


All Articles