Skip to content

Commit 60cee7f

Browse files
committed
Merge pull request mongodb#1829 from ahmedsayedabdelsalam/master
fix filtering with operator not like issue
2 parents 22996c2 + e2c9762 commit 60cee7f

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,13 @@ protected function compileWhereBasic(array $where)
978978
{
979979
extract($where);
980980

981-
// Replace like with a Regex instance.
982-
if ($operator == 'like') {
983-
$operator = '=';
981+
// Replace like or not like with a Regex instance.
982+
if (in_array($operator, ['like', 'not like'])) {
983+
if ($operator === 'not like') {
984+
$operator = 'not';
985+
} else {
986+
$operator = '=';
987+
}
984988

985989
// Convert to regular expression.
986990
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));

tests/QueryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ public function testLike(): void
7171
$this->assertCount(1, $users);
7272
}
7373

74+
public function testNotLike(): void
75+
{
76+
$users = User::where('name', 'not like', '%doe')->get();
77+
$this->assertCount(7, $users);
78+
79+
$users = User::where('name', 'not like', '%y%')->get();
80+
$this->assertCount(6, $users);
81+
82+
$users = User::where('name', 'not LIKE', '%y%')->get();
83+
$this->assertCount(6, $users);
84+
85+
$users = User::where('name', 'not like', 't%')->get();
86+
$this->assertCount(8, $users);
87+
}
88+
7489
public function testSelect(): void
7590
{
7691
$user = User::where('name', 'John Doe')->select('name')->first();

0 commit comments

Comments
 (0)