Reputation: 45491
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?
Upvotes: 2
Views: 5063
Reputation: 121
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")
);
Upvotes: 1
Reputation: 6751
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"));
Upvotes: 0
Reputation: 83081
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.
Upvotes: 4
Reputation: 3150
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.
Upvotes: 4