Skip to content

Commit f67761f

Browse files
authored
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.
1 parent 010ac2a commit f67761f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

packages/db-mongodb/src/find.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ export const find: Find = async function find(
118118
collection,
119119
collectionConfig,
120120
joins,
121-
limit,
122121
locale,
123122
query,
124123
})

packages/db-mongodb/src/queryDrafts.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
115115
collection,
116116
collectionConfig,
117117
joins,
118-
limit,
119118
locale,
120119
projection,
121120
query: versionQuery,

test/joins/int.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,28 @@ describe('Joins Field', () => {
831831
expect(res.group.relatedPosts.docs).toBeDefined()
832832
expect(res.group.camelCasePosts.docs).toBeDefined()
833833
})
834+
835+
it('should have correct totalDocs', async () => {
836+
for (let i = 0; i < 50; i++) {
837+
await payload.create({ collection: categoriesSlug, data: { name: 'totalDocs' } })
838+
}
839+
840+
const count = await payload.count({
841+
collection: categoriesSlug,
842+
where: { name: { equals: 'totalDocs' } },
843+
})
844+
expect(count.totalDocs).toBe(50)
845+
846+
const find = await payload.find({
847+
collection: categoriesSlug,
848+
limit: 5,
849+
where: { name: { equals: 'totalDocs' } },
850+
})
851+
expect(find.totalDocs).toBe(50)
852+
expect(find.docs).toHaveLength(5)
853+
854+
await payload.delete({ collection: categoriesSlug, where: { name: { equals: 'totalDocs' } } })
855+
})
834856
})
835857

836858
async function createPost(overrides?: Partial<Post>, locale?: Config['locale']) {

0 commit comments

Comments
 (0)