apache / apache/grails-core

MongoDB: count over an OR disjunction (countBy*Or*) under-counts

Open
#15,789 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Summary
On MongoDB, a count dynamic finder over an OR disjunction (e.g. `countByTypeOrName`) under-counts: it returns a single branch's count instead of the distinct union.

### Reproduce
`grails-data-mongodb` `DisjunctionQuerySpec`:

```groovy
Pet.countByTypeOrName(dogType, 'Jack') == 3 // actually returns 1
```

The test data has 3 pets matching `type == dog OR name == 'Jack'`. The equivalent **list** query is correct:

```groovy
Pet.findAllByTypeOrName(dogType, 'Jack').size() == 3 // passes
```

So the query/criteria are correct — only the **count aggregation** over the disjunction is wrong. The MongoDB count uses a `$group { _id: 1, count: { $sum: 1 } }` aggregation.

### Not a regression
This is pre-existing, independent of the GormRegistry O(M+N) work: `MongoQuery` (the count executor) is byte-identical to before that rewrite, the `CountByFinder` count mechanism is equivalent, and find-over-OR works.

### Status
`DisjunctionQuerySpec > "Count all dogs or pets with the name Jack"` is marked `@PendingFeature` so it does not block the GormRegistry PR; this issue tracks the independent fix.

Related historical tracker: GPMONGODB-380.

Contributor guide

Open the contributing guide

Research direction

Start with the pending "Count all dogs or pets with the name Jack" case in DisjunctionQuerySpec and trace the count path into MongoQuery. Run the existing MongoDB test to reproduce the result of 1 versus the expected 3, then verify the count aggregation returns the distinct union while the equivalent findAllBy query remains correct.

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
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.