具有$ and,$ in和$ eq条件的Spring mongo聚合过滤器

编程入门 行业动态 更新时间:2024-10-18 20:29:38
本文介绍了具有$ and,$ in和$ eq条件的Spring mongo聚合过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 { "Field1": "ABC", "Field2": [ { "Field3": "ABC1","Field4": "REVIEWED","Field5":"True" }, { "Field3": "ABC2","Field4": "APPROVED","Field5":"True" }, { "Field3": "ABC3","Field4": "REJECTED","Field5":"True" }, { "Field3": "ABC4","Field4": "APPROVED","Field5":"False" } ] }

我想获取批准,已审核和真实记录,即

I want to fetch APPROVED,REVIEWED and True record ie.

{ "Field1": "ABC", "Field2": [ { "Field3": "ABC1","Field4": "REVIEWED","Field5":"True" }, { "Field3": "ABC2","Field4": "APPROVED","Field5":"True" } ] }

以下mongo聚合查询返回正确的结果

following mongo aggregation query returns proper result

{ "$project": { "Field1": "$Field1", "Field2": { "$filter": { "input": "$field2", "as": "fld2", "cond": { "$and": [ { "$in": [ "$$fld2.field4", [ "APPROVED", "REVIEWED" ] ] }, { "$eq": [ "$$fld2.field5", "True" ] } ] } } } } }

如何在spring mongo data db中实现以上查询?带有ArrayOperators.Filter.filter的Spring ProjectOperation不提供链接操作来完成和其他条件.

how to achieve the above query in spring mongo data db? Spring ProjectOperation with ArrayOperators.Filter.filter not providing chaining operation to do and with another condition.

推荐答案

您可以尝试像这样使用BasicDBObject

You can try using BasicDBObject like this

BasicDBObject inOp = new BasicDBObject("$in", Arrays.<Object>asList( "$$fld2.field4", Arrays.asList("APPROVED","REVIEWED"))); BasicDBObject eqOp = new BasicDBObject("$eq", Arrays.<Object>asList( "$$fld2.field5", "True")); BasicDBObject andOp = new BasicDBObject("$and", Arrays.<Object>asList(inOp, eqOp)); project("Field1") .and(new AggregationExpression() { @Override public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) { DBObject filterExpression = new BasicDBObject(); filterExpression.put("input","$field2"); filterExpression.put("as", "fld2"); filterExpression.put("cond",andOp); return new BasicDBObject("$filter", filterExpression); } }).as("Field2");

更多推荐

具有$ and,$ in和$ eq条件的Spring mongo聚合过滤器

本文发布于:2023-11-22 13:43:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   条件   eq   mongo   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!