春天mongo的聚合

时间:2014-10-31 02:19:04

标签: java spring mongodb mongodb-query aggregation-framework

我正在尝试在spring mongo图层上重新创建此查询。

db.LASTVIEWED_TEST.aggregate([{$match: {'_id' : '12070'}},
                        {$unwind:"$value"},
                         {$match:{"value.Dockey":{"$in":["390", "539","626"]}}},
                         {$group:{_id:"$_id", "value":{$push:"$value"}}}
                        ])

我已尝试使用各种方法但是我在这里看到了这些例子:

https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java

并创建了这个:

Aggregation agg = Aggregation.newAggregation(//
                match(where("entityid").is(entityId)),//
                unwind("$value"), //
                match(where("value.Dockey").in(dockeyList)),//
                group("id").push("$value").as("value")//
                );

但是这个构建器看不到识别where,unwind关键字等...而且我的编译器告诉我我的类没有这些方法。

将值传递给newAggregation构建器需要做什么。

欢呼声,

1 个答案:

答案 0 :(得分:1)

您需要在匹配中指定一个Criteria对象,通常所有值都只表示为字符串:

    Aggregation agg = newAggregation(
        match(Criteria.where("entityid").is(entityId)),
        unwind("value"),
        match(Criteria.where("value.Dockey").in(dockeyList)),
        group("_id").push("value").as("value")
    );
相关问题