Skip to content

Commit 4f90c93

Browse files
committed
fix: mongoose ilike comparism wasn capable of searching with fixed start/end letter
1 parent c4731d0 commit 4f90c93

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

packages/query-mongoose/__tests__/query/comparison.builder.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,31 @@ describe('ComparisonBuilder', (): void => {
7373
it('should build like sql fragment', (): void => {
7474
expect(createComparisonBuilder().build('stringType', 'like', '%hello%')).toEqual({
7575
stringType: {
76-
$regex: /.*hello.*/
76+
$regex: /^.*hello.*$/
7777
}
7878
})
7979
})
8080

8181
it('should build notLike sql fragment', (): void => {
8282
expect(createComparisonBuilder().build('stringType', 'notLike', '%hello%')).toEqual({
8383
stringType: {
84-
$not: { $regex: /.*hello.*/ }
84+
$not: { $regex: /^.*hello.*$/ }
8585
}
8686
})
8787
})
8888

8989
it('should build iLike sql fragment', (): void => {
9090
expect(createComparisonBuilder().build('stringType', 'iLike', '%hello%')).toEqual({
9191
stringType: {
92-
$regex: /.*hello.*/i
92+
$regex: /^.*hello.*$/i
9393
}
9494
})
9595
})
9696

9797
it('should build notILike sql fragment', (): void => {
9898
expect(createComparisonBuilder().build('stringType', 'notILike', '%hello%')).toEqual({
9999
stringType: {
100-
$not: { $regex: /.*hello.*/i }
100+
$not: { $regex: /^.*hello.*$/i }
101101
}
102102
})
103103
})

packages/query-mongoose/src/query/comparison.builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class ComparisonBuilder<Entity extends Document> {
9898

9999
private likeComparison<F extends keyof Entity>(cmp: string, val: EntityComparisonField<Entity, F>): FilterQuery<RegExp> {
100100
const regExpStr = escapeRegExp(`${String(val)}`).replace(/%/g, '.*')
101-
const regExp = new RegExp(regExpStr, cmp.includes('ilike') ? 'i' : undefined)
101+
const regExp = new RegExp(`^${regExpStr}$`, cmp.includes('ilike') ? 'i' : undefined)
102102

103103
if (cmp.startsWith('not')) {
104104
return { $not: { $regex: regExp } }

0 commit comments

Comments
 (0)