Skip to content

$near operator is not working #853

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
hlogeon opened this issue May 31, 2016 · 2 comments
Closed

$near operator is not working #853

hlogeon opened this issue May 31, 2016 · 2 comments

Comments

@hlogeon
Copy link

hlogeon commented May 31, 2016

Hi! I'm trying to run query like this:

 db.events.find({location: {$near: [101.87496361606, 3.5223726347222], $maxDistance: 20}});

And it is fine if I'm running it in mongo-shell

But when I try to convert exactly the same query I get an error.

So in Laravel I've got the next peace of code:

return $model->whereRaw([
            'location' => [
                '$near' => [$this->lng, $this->lat],
                '$maxDistance' => $this->radius,
            ],
        ]);

If I run this code I get the following:

RuntimeException in Find.php line 179:
can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { $and: [ { start_at: { $gte: new Date(1464675831000) } }, { start_at: { $lte: new Date(1467392399000) } }, { location: { $near: [ 100.558, 13.738 ], $maxDistance: 20.0 } } ] }

If I run db.events.getIndexes() I get the following list of indexes:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "venvast.events",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2d"
        },
        "ns" : "venvast.events",
        "name" : "location_2d"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2dsphere"
        },
        "ns" : "venvast.events",
        "name" : "location_2dsphere"
    }
]

Note that I've been trying to get some results with only 2 indexes in different combinations(id, 2d), (id, 2dsphere) and only after that added both(id, 2d, 2dsphere).

What am I doing wrong?

@hlogeon
Copy link
Author

hlogeon commented May 31, 2016

After I try to figure out what's going on, I found that I can apply exactly THIS code if I don't use any other where clauses and final query doesn't use $and operator. Any ideas why it can happen? Is it MongoDB issue or PHP driver may be?

@hlogeon
Copy link
Author

hlogeon commented Jun 24, 2016

So just use whereRaw() if clash same problem

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants