MongoDB text search and geoWithin search

348 views Asked by At

I know that MongoDB does not support searching by text index and geospatial index at the same time, as written here. But I saw suggestions to use $geoWithin instead of $near. I am using $geoWithin with $box geometry and it doesn't yield any results. However, when I use $geoWithin and $center geometry, it does work. What may be wrong? Is it not supported? Is it a bug? Of course that if I apply those query conditions separately (text and geo spatial), it all works.

1

There are 1 answers

0
Josef Sábl On

Although it is mentioned several times in documentation that $geoWithin does not require geospatial index. I could not make it work with $box operator.

This is not working:

db.getCollection('Article').find({
    "coordinates": {
        $geoWithin: {
            $box: [
                [13.2680001, 49.805786],
                [13.4758465, 49.6776084]
            ]
        }
    },    
    $text: {
        $search: "Lorem ipsum"
    }
})

Pretty easy workaround is to use $gte and $lte operators instead.

Working solution:

db.getCollection('Article').find({
    "coordinates.longitude": { $gte: 13.2680001, $lte: 13.4758465 },
    "coordinates.latitude": { $gte: 49.6776084, $lte: 49.805786 },
    $text: {
        $search: "Lorem ipsum"
    }
})

I, however, am not sure about performance of such query.