fix(db-mongodb): totalDocs with joins (#9056)

### What?
Fixes issue with incorrect `totalDocs` value when an aggregation is used
for `find`.
Previously, `limit: 5` for example always returned `totalDocs: 5`.

### Why?
`totalDocs` must be returned correctly.

### How?
Removes `$limit` from the pipeline, as `Model.aggregatePaginate` handles
it by itself.
This commit is contained in:
Sasha
2024-11-08 21:04:24 +02:00
committed by GitHub
parent 010ac2ac0c
commit f67761fe22
3 changed files with 22 additions and 2 deletions

View File

@@ -118,7 +118,6 @@ export const find: Find = async function find(
collection,
collectionConfig,
joins,
limit,
locale,
query,
})

View File

@@ -115,7 +115,6 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
collection,
collectionConfig,
joins,
limit,
locale,
projection,
query: versionQuery,

View File

@@ -831,6 +831,28 @@ describe('Joins Field', () => {
expect(res.group.relatedPosts.docs).toBeDefined()
expect(res.group.camelCasePosts.docs).toBeDefined()
})
it('should have correct totalDocs', async () => {
for (let i = 0; i < 50; i++) {
await payload.create({ collection: categoriesSlug, data: { name: 'totalDocs' } })
}
const count = await payload.count({
collection: categoriesSlug,
where: { name: { equals: 'totalDocs' } },
})
expect(count.totalDocs).toBe(50)
const find = await payload.find({
collection: categoriesSlug,
limit: 5,
where: { name: { equals: 'totalDocs' } },
})
expect(find.totalDocs).toBe(50)
expect(find.docs).toHaveLength(5)
await payload.delete({ collection: categoriesSlug, where: { name: { equals: 'totalDocs' } } })
})
})
async function createPost(overrides?: Partial<Post>, locale?: Config['locale']) {