I have a mongo aggregate group query:
db.wizard.aggregate(
{
$group: {
_id: "$title",
versions: { $push: {version:"$version", author:"$author", dateAdded:"$dateAdded"}}
}
})
I need this query in Java Spring-Data-MongoDB, my current solution looks like this:
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("title").
push("version").as("versions")
);
Problem is that i don't know how to add more fields to push method (version, author, dateAdded). Is it possible with Spring-Data-MongoDB?
Note: MongoDB versión 2.6 - 3.0 (compatible Java driver)
I've extended
org.springframework.data.mongodb.core.aggregation.AggregationOperation
class to implement customtoDBObject
method:In your case, it would be:
Hope this helps you.
In the new version spring-data-mongodb:2.x.x the AggregationOperation need to return Document instead of DBObject, so the updated class will be:
Plus to make it easier to be used I'll add utility interface (java 8+, for java 7 or lower you can convert this to class utils instead):
And then we can static import the interface into our class:
And use it in the aggregation pipeline together with other spring data AggregationOperation like this:
You can directly pass the BasicDbObject to any of the aggregation pipeline stage.