Skip to content

Commit 648021c

Browse files
authored
fix: group-by sticky pagination not rendering for virtual relationship fields (#14470)
### What? Fixes pagination when grouping by virtual relationship fields in collection list views. The sticky pagination toolbar now correctly appears when there are 10+ distinct values, and `totalPages` is calculated accurately. ### Why? When grouping by virtual relationship fields (e.g., `virtualTitleFromCategory` with `virtual: 'category.title'`), the pagination was broken: - `totalPages` always returned `1` even when there were 10+ distinct values - The sticky pagination toolbar at the bottom never appeared This happened because: 1. The recursive `findDistinct` call for virtual fields wasn't passing through `limit` and `page` parameters 2. MongoDB's count aggregation pipeline was missing the relationship `$lookup` stages needed to traverse virtual field paths ### How? **1. Fixed recursive findDistinct call** - Added `limit` and `page` parameters to the recursive call when handling virtual fields **2. Fixed MongoDB count aggregation** - Changed count pipeline to include the same stages as the main query (`sortAggregation`, `relationLookup`, `$unwind`, `$group`) - Ensures relationship lookups are performed before counting distinct values
1 parent d4d9622 commit 648021c

File tree

9 files changed

+546
-304
lines changed

9 files changed

+546
-304
lines changed

packages/db-mongodb/src/findDistinct.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,31 @@ export const findDistinct: FindDistinct = async function (this: MongooseAdapter,
215215
$skip: (page - 1) * args.limit,
216216
})
217217
pipeline.push({ $limit: args.limit })
218-
const totalDocs = await Model.aggregate(
219-
[
220-
{
221-
$match: query,
222-
},
223-
{
224-
$group: {
225-
_id: `$${fieldPath}`,
226-
},
227-
},
228-
{ $count: 'count' },
229-
],
218+
219+
// Build count pipeline with the same structure as the main pipeline
220+
// to ensure relationship lookups are included
221+
const countPipeline: PipelineStage[] = [
230222
{
231-
session,
223+
$match: query,
232224
},
233-
).then((res) => res[0]?.count ?? 0)
225+
...(sortAggregation.length > 0 ? sortAggregation : []),
226+
...(relationLookup?.length ? relationLookup : []),
227+
...($unwind
228+
? [
229+
{
230+
$unwind,
231+
},
232+
]
233+
: []),
234+
{
235+
$group,
236+
},
237+
{ $count: 'count' },
238+
]
239+
240+
const totalDocs = await Model.aggregate(countPipeline, {
241+
session,
242+
}).then((res) => res[0]?.count ?? 0)
234243
const totalPages = Math.ceil(totalDocs / args.limit)
235244
const hasPrevPage = page > 1
236245
const hasNextPage = totalPages > page

packages/payload/src/collections/operations/findDistinct.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ export const findDistinctOperation = async (
174174
depth: args.depth,
175175
disableErrors,
176176
field: path,
177+
limit: args.limit,
177178
locale,
178179
overrideAccess,
180+
page: args.page,
179181
populate,
180182
req,
181183
showHiddenFields,

0 commit comments

Comments
 (0)