0

Это моя MongoDB команда оболочки:MongoDB Aggregation команда Java кода

db.dais_employee.aggregate([ 
    { 
     "$redact": { 
      "$cond": { 
       "if": { 
        "$gt": [ 
         { "$subtract": [ "$modifiedon", "$createdon" ] }, 
         1000 * 60 * 60 * 24 
        ] 
       }, 
       "then": "$$KEEP", 
       "else": "$$PRUNE" 
      } 
     } 
    } 
]) 

, который работает отлично.

Мне нужно работать с этой командой MongoDB в java, и мне нужна помощь в использовании инфраструктуры агрегации MongoDB с помощью java-драйвера.

Когда я пытаюсь запустить эту команду с помощью драйвера Java:

AggregateIterable<Document> iterable = collection.aggregate(asList(
    new Document("$redact", 
     new Document("$cond", 
      new Document("if", 
       new Document("$gt", 
         asList(new Document("$subtract", 
          asList("$modifiedon", "$createdon") 
        ),1000 * 60 * 60 * 24) 
      ).append("then", "$$KEEP") 
       .append("else", "$$PRUNE") 
      ) 
     ) 
    ) 
)); 

, который генерирует исключение,

Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 15990: 'exception: this object is already an operator expression, and can't be used as a document expression (at 'then')' on server 192.168.1.127:27017. The full response is { "errmsg" : "exception: this object is already an operator expression, and can't be used as a document expression (at 'then')", "code" : 15990, "ok" : 0.0 } 
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115) 
    at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114) 
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159) 
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286) 
    at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173) 
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215) 
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206) 
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112) 
    at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:227) 
    at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:223) 
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239) 
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212) 
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:223) 
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:65) 
    at com.mongodb.Mongo.execute(Mongo.java:772) 
    at com.mongodb.Mongo$2.execute(Mongo.java:759) 
    at com.mongodb.OperationIterable.iterator(OperationIterable.java:47) 
    at com.mongodb.OperationIterable.forEach(OperationIterable.java:70) 
    at com.mongodb.AggregateIterableImpl.forEach(AggregateIterableImpl.java:117) 
    at threadpack.queryBtDates.main(queryBtDates.java:88) 

Помогите мне, чтобы узнать ошибку, или дать правильный код для запуска этого с помощью java Driver.

ответ

1

asList("$modifiedon",1000) должно быть asList("$modifiedon","$createdon") согласно действующему трубопроводу, предоставленному вами.

Как вы это сделали. Видите, проблема в том, что вы добавляете «then» и «else» в документ «$ gt», а не «if».

Таким образом, вместо:

AggregateIterable<Document> iterable = collection.aggregate(

asList(new Document("$redact", 
     new Document("$cond", 
      new Document("if", 
       new Document("$gt", 
         asList(new Document("$subtract", 
          asList("$modifiedon", "$createdon") 
        ),1000 * 60 * 60 * 24) 
      ).append("then", "$$KEEP") 
       .append("else", "$$PRUNE") 
      ) 
     ) 
    ) 
)); 

Вы должны сделать:

AggregateIterable<Document> iterable = collection.aggregate(

asList(new Document("$redact", 
     new Document("$cond", 
      new Document("if", 
       new Document("$gt", 
         asList(new Document("$subtract", 
          asList("$modifiedon", "$createdon") 
        ),1000 * 60 * 60 * 24) 
      ) 
      ).append("then", "$$KEEP") 
       .append("else", "$$PRUNE") 
     ) 
    ) 
)); 
+0

@Jatin Чаудхари спасибо, теперь работает правильно. – radhakrishnan