spring mongo聚合过滤器

时间:2018-04-29 08:57:29

标签: java spring mongodb spring-data-mongodb

我正在使用spring在mongodb上运行一个同意查询。 我会为我的投影添加一些过滤器。所以我想用spring编写流程代码

db.mycollection.aggregate( [
  {$match: {"_id" : { "$binary" : "sU7XGDnFYz53KHVhP+sZlQ==", "$type" : "03" } }},
  {$project: {
         myarray: {
            $filter: {
               input: "$myarray",
               as: "item",
               cond: { $in: [ "$$item._id", ["JshGyMImCsSceiPqqCCinlAtTrIkwvlx", "0000022211"] ] }
            }
         },
         "colomn" :1,

          myarray2: {
            $filter: {
               input: "$myarray2",
               as: "item",
               cond: { $in: [ "$$item.name", ["name1", "name2"] ] }
            }
         }
      }
  }
] );

我尝试用Spring Mongo写作

    /*<arrayname,list ids>*/
    Map<String, List<Object>> arraysAggregationMap = new HashMap<>();

    ...


ProjectionOperation projectionOperation = project();

    //adding projections

    for (String f: fields) {
      projectionOperation = projectionOperation.and(f).as(f);
    }

// filters
    for (Entry<String, List<Object>> entry: arraysAggregationMap.entrySet()
        ) {
      projectionOperation =  projectionOperation.and(filter(entry.getKey()).as("item").by(
          in("item"+"._id",entry.getValue()).toString())).as(entry.getKey());

    }
    Aggregation aggregation;

    if(fields.size()>0 ||arraysAggregationMap.size() >0) aggregation = newAggregation(match(creteria),projectionOperation);
    else  aggregation = newAggregation(match(creteria));


AggregationResults results = mongoOperation.aggregate(aggregation,entityClass,Document.class);

但是$filter的效果并不一样。

0 个答案:

没有答案