-
Type: Bug
-
Resolution: Won't Do
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: Laravel
gw-patrick has created Issue #3063: paginate() method thows an exception when query contains 'near' in laravel-mongodb. This Jira ticket was filed by GromNaN
Issue Text:
- Laravel-mongodb Version: 4.7.0
- PHP Version: 8.3.7
- Database Driver & Version: mongo:5.0.8 (via docker image)
Description:
Running a query to find a record near a given position results in an error -
`MongoDB\Driver\Exception\CommandException $geoNear, $near, and $nearSphere are not allowed in this context..`
Steps to reproduce
1. Create a collection named 'stores' using the json below -
```
[{
"_id":
,
"position": [
9.224596977233887,
52.03082275390625
],
"name": "Store A",
"created_at":
,
"updated_at":
},
{
"_id":
,
"position": [
9.224596977233887,
52.03082275390625
],
"name": "Store B",
"created_at":
,
"updated_at":
},
{
"_id":
,
"position": [
9.224596977233887,
52.03082275390625
],
"name": "Store C",
"created_at":
,
"updated_at":
}]
```
2. Run the query below (on laravel tinker) to get a paginated list of stores near a location
```
$query = DB::connection('mongo')
->collection('stores')
->where('position', 'near', [
'$geometry' => [
'type' => 'Point',
'coordinates' => [
9.3731451034546,
52.1019308
],
],
'$maxDistance' => 50,
]);
$query->paginate(); // this results in error
// $query->get(); // this works
```
Expected behaviour
Get a paginated list of records.
Actual behaviour
An exception is thrown: - `MongoDB\Driver\Exception\CommandException $geoNear, $near, and $nearSphere are not allowed in this context.`
<details><summary><b>Stacktrace</b>:</summary>
MongoDB\Driver\Exception\CommandException $geoNear, $near, and $nearSphere are not allowed in this context.
–
() at vendor/mongodb/mongodb/src/Operation/Aggregate.php:347
MongoDB\Driver\Server->executeReadCommand() at vendor/mongodb/mongodb/src/Operation/Aggregate.php:347
MongoDB\Operation\Aggregate->executeCommand() at vendor/mongodb/mongodb/src/Operation/Aggregate.php:254
MongoDB\Operation\Aggregate->execute() at vendor/mongodb/mongodb/src/Operation/CountDocuments.php:125
MongoDB\Operation\CountDocuments->execute() at vendor/mongodb/mongodb/src/Collection.php:307
MongoDB\Collection->countDocuments() at vendor/mongodb/laravel-mongodb/src/Collection.php:49
MongoDB\Laravel\Collection->__call() at vendor/mongodb/laravel-mongodb/src/Query/Builder.php:462
MongoDB\Laravel\Query\Builder->getFresh() at vendor/mongodb/laravel-mongodb/src/Query/Builder.php:239
MongoDB\Laravel\Query\Builder->get() at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2904
Illuminate\Database\Query\Builder->runPaginationCountQuery() at vendor/mongodb/laravel-mongodb/src/Query/Builder.php:932
MongoDB\Laravel\Query\Builder->runPaginationCountQuery() at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2863
Illuminate\Database\Query\Builder->getCountForPagination() at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:922
Illuminate\Database\Eloquent\Builder->paginate() at eval()'d code:1
eval() at vendor/psy/psysh/src/ExecutionLoopClosure.php:52
Psy{closure}() at vendor/psy/psysh/src/ExecutionClosure.php:89
Psy\ExecutionClosure->execute() at vendor/psy/psysh/src/Shell.php:383
Psy\Shell->doInteractiveRun() at vendor/psy/psysh/src/Shell.php:354
Psy\Shell->doRun() at vendor/symfony/console/Application.php:175
Symfony\Component\Console\Application->run() at vendor/psy/psysh/src/Shell.php:329
Psy\Shell->run() at vendor/laravel/tinker/src/Console/TinkerCommand.php:85
Laravel\Tinker\Console\TinkerCommand->handle() at vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
Illuminate\Container\BoundMethod::Illuminate\Container{closure}() at vendor/laravel/framework/src/Illuminate/Container/Util.php:41
Illuminate\Container\Util::unwrapIfClosure() at vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
Illuminate\Container\BoundMethod::callBoundMethod() at vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35
Illuminate\Container\BoundMethod::call() at vendor/laravel/framework/src/Illuminate/Container/Container.php:662
Illuminate\Container\Container->call() at vendor/laravel/framework/src/Illuminate/Console/Command.php:211
Illuminate\Console\Command->execute() at vendor/symfony/console/Command/Command.php:326
Symfony\Component\Console\Command\Command->run() at vendor/laravel/framework/src/Illuminate/Console/Command.php:180
Illuminate\Console\Command->run() at vendor/symfony/console/Application.php:1096
Symfony\Component\Console\Application->doRunCommand() at vendor/symfony/console/Application.php:324
Symfony\Component\Console\Application->doRun() at vendor/symfony/console/Application.php:175
Symfony\Component\Console\Application->run() at vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:201
Illuminate\Foundation\Console\Kernel->handle() at artisan:35
</details>