From c96985be0c14fcff768e036de96ebef3caa24d1c Mon Sep 17 00:00:00 2001 From: James Date: Mon, 25 Jul 2022 16:48:45 -0400 Subject: [PATCH 1/2] fix: #806, allow partial word matches using 'like' operator --- src/mongoose/sanitizeFormattedValue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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' }; From b79ae006030419ed13c4fe64937cc4606aa756f8 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 25 Jul 2022 16:56:45 -0400 Subject: [PATCH 2/2] test: adds like query partial word match test --- test/collections-rest/int.spec.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 });