mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Home Page:https://www.mongodb.com/compatibility/mongodb-laravel-integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`whereAny` and `whereAll` query Error

ilejohn-official opened this issue · comments

  • Laravel-mongodb Version: 4.1.x-dev
  • PHP Version: 8.2.14
  • Database Driver & Version:

Description:

The whereAny and whereAll query are not generating accurate mongo queries as it does in SQL. https://laravel.com/docs/10.x/queries#where-any-all-clauses

Steps to reproduce

  1. perform a whereAny or whereAll query on a collection
  2. chain toMql() to see the query output

Expected behaviour

for example a whereAny query

Model::whereAny(['title','text','shortcut','first_line'], 'like', '%-p%')->toMql();

should look like this

[
 'find' => [
     '$or' => [
            ['title' => new \MongoDB\BSON\Regex('^.*\-p.*$', 'i')],
            ['text' => new \MongoDB\BSON\Regex('^.*\-p.*$', 'i')],
            ['shortcut' => new \MongoDB\BSON\Regex('^.*\-p.*$', 'i')],
            ['first_line' => new \MongoDB\BSON\Regex('^.*\-p.*$', 'i')],
      ],
 ],
]
  

Actual behaviour

Instead this is what I see

[
 'find' => [
    'any' => [
            0 => ['title' => true],
            1 => ['text' => true],
            2 => ['shortcut' => true],
            3 => ['first_line' => true],
        ],
  ]
 ]

Thanks for the feedback @ilejohn-official. This new methods have been added last week in Laravel (by laravel/framework#50344). Would you like to open a PR and add it to laravel-mongodb 4.2?

The new methods will be in MongoDB\Laravel\Query\Builder and the tests can be added to MongoDB\Laravel\Tests\Query\BuilderTest

This feature request is tracked in PHPORM-159

You need to upgrade to Laravel v10.47+ to get this new feature. Otherwise you fallback to the the dynamicWhere mechanism that doesn't work as you expect.

So the PR is no longer needed, right?

I opened #2763 to add tests on this feature. But nothing needs to be done to support this feature as soon as you upgrade to a version of Laravel that supports it.