Will
Will

Reputation: 8631

Aggregation on spring mongo

I am trying to recreate this query on spring mongo layer.

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

I have made some attempts using various methods however I came across the examples here:

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

and created this:

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

However this builder does not see to recognise the where, unwind keywords etc... and my compiler tells me my class does not have these methods.

What do I need to do to pass values to the newAggregation builder.

cheers,

Upvotes: 1

Views: 1015

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151092

You need to specify a Criteria object in the match, and generally all values are just represented as strings:

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

Upvotes: 1

Related Questions