Skip to content

Conversation

@ttrelle
Copy link
Contributor

@ttrelle ttrelle commented Feb 8, 2013

Basic implementation for the aggregation framework introduced in MongoDB 2.2.

I added an aggregate() method to the MongoTemplate and provided an abstraction AggregationPipeline of the input parameters exposing a fluent API. All pipeline operations can be used via a raw JSON string so the full aggregation API is supported.

Some pipeline operations ($project and $group) offer a very rich DSL (having a gazillion of expression operators, see http://docs.mongodb.org/manual/reference/aggregation/#aggregation-expression-operators). These are not (yet) implemented in a type safe way and with a fluent API, but I started this with an abstraction "Project".

@odrotbohm
Copy link
Member

Tobias' CLA number: 46120130327045545

@odrotbohm
Copy link
Member

I've merged Tobias work into a feature branch DATAMONGO-586. I put a round of code polish into it as well. In case anyone wants to contribute further, please take it from there. I'll merge this into master as soon as we have a MongoDB 2.2 in place on our CI server.

In the meantime, simply build the branch locally with a MongoDB 2.2 or newer running. Feedback appreciated!

@nickolayrusev
Copy link

Hello, I have a questions about aggregation framework implementation.
Let's say we have collections with article items:
{
"_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:

 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 display only comments ordered by "createdAt" field.
Thanks in advance

@odrotbohm
Copy link
Member

Would you mind posting this to the forums? The pull request is not the right place to discuss issues like this.

@nickolayrusev
Copy link

Ok, sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants