MongoDB $aggregate $push multiple fields in Java Spring Data
You can directly pass the BasicDbObject to any of the aggregation pipeline stage.
Aggregation agg = newAggregation(
group("title").
push(new BasicDBObject
("version", "$version").append
("author", "$author").append
("dateAdded", "$dateAdded")).as("versions"));
Note: MongoDB versión 2.6 - 3.0 (compatible Java driver)
I've extended org.springframework.data.mongodb.core.aggregation.AggregationOperation
class to implement custom toDBObject
method:
public class GenericAggregationOperation implements AggregationOperation {
private String operator;
private DBObject query;
/**
* Default constructor.
*
* @param operator MongoDB operator ($group, $sort, $project, etc..)
* @param query MongoDB aggregation query step string
*/
public GenericAggregationOperation(String operator, String query) {
this(operator, (DBObject) JSON.parse(query));
}
/**
* Default constructor.
*
* @param operator MongoDB operator ($group, $sort, $project, etc..)
* @param query MongoDB aggregation query step DBObject
*/
public GenericAggregationOperation(String operator, DBObject query) {
this.operator = operator;
this.query = query;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return new BasicDBObject(operator, query);
}
}
In your case, it would be:
List<AggregationOperation> list = new ArrayList<AggregationOperation>();
lista.add(new GenericAggregationOperation("$group", "{ \"_id\": \"$title\", \"versions\": { \"$push\": { \"version\":\"$version\", \"author\":\"$author\", \"dateAdded\":\"$dateAdded\"}} }"));
TypedAggregation<EpisodeIndexDto> agg = Aggregation.newAggregation(
YourClassTitleVersion.class, list);
mongoOperations.aggregate(agg, YourClassTitleVersion.class,
YourClassTitleVersion.class).getMappedResults();
Hope this helps you.