yuceel
yuceel

Reputation: 1951

using $addToset with java morphia aggregation

I have mongodb aggregation query and it works perfectly in shell. How can i rewrite this query to use with morphia ?

org.mongodb.morphia.aggregation.Group.addToSet(String field) accepts only one field name but i need to add object to the set.

Query:

......aggregate([
{$group: 
    {"_id":"$subjectHash",
        "authors":{$addToSet:"$fromAddress.address"},
 ---->> "messageDataSet":{$addToSet:{"sentDate":"$sentDate","messageId":"$_id"}},
        "messageCount":{$sum:1}}},
        {$sort:{....}},
        {$limit:10},
        {$skip:0}
        ])

Java code:

  AggregationPipeline aggregationPipeline = myDatastore.createAggregation(Message.class)
                    .group("subjectHash",
                            grouping("authors", addToSet("fromAddress.address")),
 --------??????------>>     grouping("messageDataSet", ???????),
                            grouping("messageCount", new Accumulator("$sum", 1))
                    ).sort(...)).limit(...).skip(...);

Upvotes: 1

Views: 995

Answers (2)

yuceel
yuceel

Reputation: 1951

Thanks for your answer, I can guess that according to source code. :(

I don't want to use spring-data or java-driver directly (for this project) so I changed my document representation.

Added messageDataSet object which contains sentDate and messageId (and some other nested objects) (these values become duplicated in a document which is a bad design).

Aggregation becomes : "messageDataSet":{$addToSet:"$messageDataSet"},

and Java code is: grouping("messageDataSet", addToSet("messageDataSet")),

This works with moprhia. Thanks.

Upvotes: 0

evanchooly
evanchooly

Reputation: 6233

That's currently not supported but if you'll file an issue I'd be happy to include that in an upcoming release.

Upvotes: 1

Related Questions