diff --git a/src/mongoose/sanitizeFormattedValue.ts b/src/mongoose/sanitizeFormattedValue.ts index e49fd0791e..102ba11be4 100644 --- a/src/mongoose/sanitizeFormattedValue.ts +++ b/src/mongoose/sanitizeFormattedValue.ts @@ -98,7 +98,7 @@ export const sanitizeQueryValue = (schemaType: SchemaType, path: string, operato if (operator === 'like' && typeof formattedValue === 'string') { const words = formattedValue.split(' '); const regex = words.reduce((pattern, word, i) => { - return `${pattern}(?=.*\\b${word}\\b)${i + 1 === words.length ? '.+' : ''}`; + return `${pattern}(?=.*\\b${word}.*\\b)${i + 1 === words.length ? '.+' : ''}`; }, ''); formattedValue = { $regex: new RegExp(regex), $options: 'i' }; diff --git a/test/collections-rest/int.spec.ts b/test/collections-rest/int.spec.ts index 81218b0cd1..9f964fabe8 100644 --- a/test/collections-rest/int.spec.ts +++ b/test/collections-rest/int.spec.ts @@ -357,7 +357,7 @@ describe('collections-rest', () => { it('like', async () => { const post1 = await createPost({ title: 'prefix-value' }); - await createPost(); + const { status, result } = await client.find({ query: { title: { @@ -371,6 +371,22 @@ describe('collections-rest', () => { expect(result.totalDocs).toEqual(1); }); + it('like - partial word match', async () => { + const post = await createPost({ title: 'separate words should partially match' }); + + const { status, result } = await client.find({ + query: { + title: { + like: 'words partial', + }, + }, + }); + + expect(status).toEqual(200); + expect(result.docs).toEqual([post]); + expect(result.totalDocs).toEqual(1); + }); + it('exists - true', async () => { const postWithDesc = await createPost({ description: 'exists' }); await createPost({ description: undefined });