2

this is the query I want to perform:

db['applications'].aggregate(
 {$project: {key:1, _id:0}}
 ,{$group: { _id:null, keyList: {$addToSet:"$key"}}} );

This is the code that I am using:

 Aggregation agg = newAggregation(
            project("key"),
            group("key").addToSet("key").as("keylist")
    );

However, the result produced is this:

Executing aggregation: 
{ "aggregate" : "applications" , "
  pipeline" : [ 
 { "$project" : { "key" : "$key"}} , 
 { "$group" : { "_id" : "$key" , "keylist" : { "$addToSet" : "$key"}}}
]}

Why is the _id key in the group set to the arguments of 'group'. How do I make it null?
How do i get Spring MongoData to produce the query I want?

2
  • 2
    FYI: This isn't directly related to your question, but you don't need to explicitly add the $project phase before the $group. MongoDB will automatically determine which fields are needed from the source document and only extract those. (I'm the main dev on the aggregation framework)
    – mstearn
    Sep 23, 2013 at 17:59
  • Just out of curiousity have that helped my answer?
    – attish
    Sep 24, 2013 at 8:02

4 Answers 4

4

Unfortunately for the group phase you cannot pass null look at the documentation.

public static GroupOperation group(String... fields)
Creates a new GroupOperation for the given fields.
Parameters:
fields - must not be null.
Returns:

However you can achive the same with selecting a field name for the group id which is not exists in the collection. Not beautiful but should work.

Aggregation agg = newAggregation(
            project("key"),
            group("ANYSTRINGWHICHISNOTFIELDNAME").addToSet("key").as("keylist")
    );

You do not have to care about the project phase it is good to have smaller data in the pipeline but the result will be the same this way.

1
  • didn't work with spring data mongo 1.10.8.RELEASE, so using empty group() instead
    – boly38
    Mar 9, 2018 at 9:47
4

You can use the method taking a String... and simply pipe in no arguments:

group().addToSet("key").as("keylist")

In the currently released version this renders as:

$group: { _id : {}, keyList : { $addToSet : "$key" }}

While this is not 100% in line with the documentation it does the same as _id : null. We've filed and fixed DATAMONGO-759 which will ship in 1.3.2 and 1.4 M1 of Spring Data Commons.

1

As of the current version of Spring Data MongoDB if you do not specify parameters to group it will group by null.

Aggregation agg = newAggregation(
            project("key"),
            group().addToSet("key").as("keylist")
    );
0

Didnt work for me , BUT what did work is to declare in you collection dummy property like this:

Class Model{
...
...
private Integer dummyNotExistsProp
}

and then this is stupid query without matchin and projection but just to demonstrate the solution to the problem:

Aggregation agg = Aggregation.newAggregation(
                Aggregation.match(Aggregation.group("dummyNotExistsProp").count().as("totalobjs"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.