MongoDB $aggregate $push multiple fields in Java Spring Data

quiros picture quiros · Sep 8, 2016 · Viewed 11.6k times · Source

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?

Answer

s7vr picture s7vr · Nov 24, 2016

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"));