Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

Spring Data MongoDB aggregation framework implementation - exceptions

$
0
0
Hello all,
I'm trying to use the new implementation of the Mongo Aggregation framework by Spring Data team I'm using the spring-data version 1.3.0.RC1 ! and for reference I'm looking at https://jira.springsource.org/browse/DATAMONGO-586
Here is my problem:

Let's say we have collections with article items:
Code:

{
"_id": {
"$oid": "5214b5d529ee12460939e2ba"
},
"title": "this is my title",
"author": "bob",
"pageViews": 5,
"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
}
],
"other": {
"foo": 5
}
}
.....

How I can make an aggregation query like this:
db.articles.aggregate( {$project:{author:1, comments:1}} , {$unwind:"$comments"} , {$sort:{"comments.createdAt":-1}} , {$group: {_id:null,comments:{$push:"$comments"}}} );

I try with the following code snippet but every time exceptions are thrown:

Code:

        Aggregation agg = newAggregation( //
                project().andInclude("author").andInclude("comments"), //this should make the project part : {$project:{author:1, comments:1}} - actual result: { "comments" : "$comments" , "author" : "$author"}
                unwind("comments"),//second part :  {$unwind:"$comments"} - this is correct
                group("_id","comments").push("comments").as("comments") - throws invalid reference "_id"...
                //sort(Direction.ASC,"comments.createdAt") - this didn't work with nested array properties
        );

I want to transform articles collection to be displayed only with comments ordered by "createdAt" field.
I don't know if my approach is wrong or the aggregation framework is not yet fully implemented.
Thanks in advance

Viewing all articles
Browse latest Browse all 128

Trending Articles