diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index c706b24a4..70b857293 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -978,9 +978,13 @@ protected function compileWhereBasic(array $where) { extract($where); - // Replace like with a Regex instance. - if ($operator == 'like') { - $operator = '='; + // Replace like or not like with a Regex instance. + if (in_array($operator, ['like', 'not like'])) { + if ($operator === 'not like') { + $operator = 'not'; + } else { + $operator = '='; + } // Convert to regular expression. $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b697555cc..ab36d1886 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -71,6 +71,21 @@ public function testLike(): void $this->assertCount(1, $users); } + public function testNotLike(): void + { + $users = User::where('name', 'not like', '%doe')->get(); + $this->assertCount(7, $users); + + $users = User::where('name', 'not like', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not LIKE', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not like', 't%')->get(); + $this->assertCount(8, $users); + } + public function testSelect(): void { $user = User::where('name', 'John Doe')->select('name')->first();