我正在尝试构建一个如下所示的过滤器表达式:
{ $expr:{ $gt:['$bal1', '$bal2'] } }
使用Filter.expr函数:
Bson filter = Filters.expr( gt("$bal1", "$bal2") );
BsonDocument doc = filter.toBsonDocument(BsonDocument.class, collection.getCodecRegistry());
System.out.println(doc.toJson());
这会生成以下json:
{ "$expr" : { "$bal1" : { "$gt" : "$bal2" } } }
显然这不对。有没有办法使用Java静态导入接口创建此查询,还是我不得不手动构造字符串?我是Mongo的新手,我无法想象每个人都在手工制作弦乐 - 任何指导都会非常感激。
MongoDB Java驱动程序3.6.1
答案 0 :(得分:1)
$expr
采用聚合比较函数。因此,您无法使用常规查询构建器。
不幸的是,您只需使用Document.parse
来解析聚合比较字符串。
Bson filter = Filters.expr( Document.parse(" { $gt: [ \"$bal1\" , \"$bal2\"] } ") );
比较query operators
与aggregation comparison operators
。
检查实施jira以获取更多详细信息。