Skip to content

Commit dc69e2c

Browse files
authored
fix(db-mongodb): db.find default limit to 0 (#8376)
Fixes an error anytime a `db.find` is called on documents with joins without a set `limit` by defaulting the limit to 0.
1 parent 19e2f10 commit dc69e2c

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

packages/db-mongodb/src/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const find: Find = async function find(
1515
{
1616
collection,
1717
joins = {},
18-
limit,
18+
limit = 0,
1919
locale,
2020
page,
2121
pagination,

packages/db-mongodb/src/utilities/buildJoinAggregation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ export const buildJoinAggregation = async ({
111111
input: `$${as}.docs`,
112112
},
113113
}, // Slicing the docs to match the limit
114-
[`${as}.hasNextPage`]: {
115-
$gt: [{ $size: `$${as}.docs` }, limitJoin || Number.MAX_VALUE],
116-
}, // Boolean indicating if more docs than limit
114+
[`${as}.hasNextPage`]: limitJoin
115+
? { $gt: [{ $size: `$${as}.docs` }, limitJoin] }
116+
: false,
117+
// Boolean indicating if more docs than limit
117118
},
118119
},
119120
)

test/joins/int.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ describe('Joins Field', () => {
117117
it('should populate joins using find', async () => {
118118
const result = await payload.find({
119119
collection: 'categories',
120+
where: {
121+
id: { equals: category.id },
122+
},
120123
})
121124

122125
const [categoryWithPosts] = result.docs
@@ -126,6 +129,29 @@ describe('Joins Field', () => {
126129
expect(categoryWithPosts.group.relatedPosts.docs[0].title).toBe('test 14')
127130
})
128131

132+
it('should not error when deleting documents with joins', async () => {
133+
const category = await payload.create({
134+
collection: 'categories',
135+
data: {
136+
name: 'category with post',
137+
},
138+
})
139+
140+
const post = await createPost({
141+
category: category.id,
142+
})
143+
144+
const result = await payload.delete({
145+
collection: 'categories',
146+
// id: category.id,
147+
where: {
148+
id: { equals: category.id },
149+
},
150+
})
151+
152+
expect(result.docs[0].id).toStrictEqual(category.id)
153+
})
154+
129155
describe('Joins with localization', () => {
130156
let localizedCategory: Category
131157

0 commit comments

Comments
 (0)