2016-11-28 25 views
0

在MongoDB的Java Driver中,它是否支持直接读取mongo shell命令?我正在为Java中的Mongo dba创建一个Mongo客户端,并希望允许用户输入任何种类的Mongo shell命令,但我不确定是否可以通过Mongo Java Driver来完成所有dba任务。 例如,当用户在mongo shell中输入show dbsshow collections时,将显示数据库或集合列表。另外dba可以键入rs.status(), rs.config()命令来执行一些管理任务。我不知道mongo java驱动程序是否支持这种输入。我知道我可以使用来自Java驱动程序的一些API,如database.runCommand(new Document(...));,但它在shell命令和Java方法之间需要一些转换任务。是否有更好的方法来做到这一点?如何使用Mongo Java Driver执行某种管理命令?

+0

什么是落后发送shell命令的想法?为什么你不能执行等效的java API方法? – Veeram

回答

0
protected CommandResult runReplicaSetStatusCommand(final Mongo pMongo) { 
     // Check to see if this is a replica set... if not, get out of here. 
     final CommandResult result = pMongo.getDB("admin").command(
       new BasicDBObject("replSetGetStatus", 1)); 

     final String errorMsg = result.getErrorMessage(); 

     if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) { 
      System.err 
        .println("This is not a replica set - not testing secondary reads"); 
      return null; 

得到满例如:http://massapi.com/source/bitbucket/12/14/1214103355/src/test/java/com/mongodb/util/TestCase.java.html#307

+0

这是否意味着我可以通过Java驱动程序使用所有管理员命令? –

+0

从我看到的。其中大部分是肯定的。你只需要玩一点。你对此有什么特别感兴趣? –

+0

我有兴趣阅读纯mongo shell命令,如“show dbs”,“rs.status()”,“rs.config()”,然后将它们直接放入Java驱动程序类。我不确定这是否可能。 –