I found this article in Spring Forum which obviously dicusses partly the same problem, but has no answer to my question.
Given the following document...
{
"_id": { "$oid": "5214b5d529ee12460939e2ba"},
"title": "this is my title",
"tags": [ "fun", "sport" ],
"comments": [
{
"author": "alex",
"text": "this is cool",
"createdAt": 1
},
{
"author": "sam",
"text": "this is bad",
"createdAt": 2
},
{
"author": "jenny",
"text": "this is bad",
"createdAt": 3
}
]
}
... I want to do this aggregation (Javascript) ...
//This is as concise as possible to focus on the actual problem which is the sort operation when ported to Spring!
db.articles.aggregate(
{$unwind:"$comments"},
//do more like match, group, etc...
{$sort:{"comments.createdAt":-1}} //Sort descending -> here the problem occurs in Spring (works in Javascript!)
);
... but with Spring -> Throws Invalid Reference!
Aggregation agg = newAggregation(
unwind("comments"),
sort(Direction.DESC, "comments.createdAt") //Throws invalid reference 'comments.createdAt'!
//How can I make this work?
);
Of course I can do it with the native Java-Driver and without usage of Spring's MongoTemplate but I don't like this approach very much. What can I do to make this exact aggregation work with Spring?
I am using the current Version 1.4.0.RELEASE.