groupProperty not work well
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
I use groupProperty in withCriteria, like this:
{
projections {
groupProperty("type")
property("type")sum("isHUPDAT")//, "numOfHUPDAT")
sum("duration")//, "numOfDuration")
// countDistinct("logicalLogID") //"count"
}
}
Howerver the group not work well, I turn on the mongo profile to log execution, I found the mongo aggregation statement is
`
[ { $project: { type: 1, isHUPDAT: 1, duration: 1 } }, { $group: { _id: { type: "$type", _id: "$_id" }, sum_isHUPDAT: { $sum: "$isHUPDAT" }, sum_duration: { $sum: "$duration" } } } ]
`
Excatly, I just want to group by `type`, so correct group id should be like this ` { _id: { type: "$type" }`.
So I trace the gorm mongo source code,
In `grails-datastore-gorm-mongodb-6.1.6.RELEASE`
org.grails.datastore.mapping.mongo.query.MongoQuery
there are codes as following:
projectProjectionHandlers.put(PropertyProjection.class, new ProjectionHandler() {
@Override
public String handle(PersistentEntity entity, Document projectObject, Document groupBy, PropertyProjection projection) {
String property = projection.getPropertyName();
projectObject.put(property, 1);
Document id = getIdObjectForGroupBy(groupBy);
String projectedValueKey = property.replace('.', '_');
id.put(projectedValueKey, "$" + property);
// we add the id to the grouping to make it not distinct
id.put(MongoEntityPersister.MONGO_ID_FIELD, "$" + MongoEntityPersister.MONGO_ID_FIELD);
return projectedValueKey;
}
});
You will alway and _id if I use group, which will make mistake.
// we add the id to the grouping to make it not distinct
id.put(MongoEntityPersister.MONGO_ID_FIELD, "$" + MongoEntityPersister.MONGO_ID_FIELD);
I don't know why? is it a bug or how can i do ?
Contributor guide
Research direction
Start in grails-datastore-gorm-mongodb-6.1.6.RELEASE's org.grails.datastore.mapping.mongo.query.MongoQuery, especially the PropertyProjection handler and getIdObjectForGroupBy. Reproduce the withCriteria projection and compare the generated aggregation with the requested grouping. Done means groupProperty("type") produces a group key containing type without the document _id, while the sums still work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, mongodb
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100