mongo java aggregate,MongoDB Aggregation命令转换为Java代码

This is my mongodb shell command:

db.dais_employee.aggregate([

{

"$redact": {

"$cond": {

"if": {

"$gt": [

{ "$subtract": [ "$modifiedon", "$createdon" ] },

1000 * 60 * 60 * 24

]

},

"then": "$$KEEP",

"else": "$$PRUNE"

}

}

}

])

which works fine.

I need to work this MongoDB command in java and I need help for using MongoDB Aggregation framework with java driver.

When I am trying to run this command using java driver:

AggregateIterable 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")

)

)

)

));

which throws an Exception,

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)

Help me to find out the error, or give correct code to run this command using java Driver.

解决方案

asList("$modifiedon",1000) should be asList("$modifiedon","$createdon") as per the working pipeline provided by you.

As you have done the edit. Can see that the problem is you are appending the "then" and "else" to the "$gt" document rather than "if".

So instead of:

AggregateIterable 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")

)

)

)

));

You should do :

AggregateIterable 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值