Skip to content

$near operator is not working #853

Closed
@hlogeon

Description

@hlogeon

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions