具有$ and,$ in和$ eq条件的Spring mongo聚合过滤器 [英] Spring mongo aggregation filter with $and, $in and $eq conditions

查看:190
本文介绍了具有$ 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聚合过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆