randomKek
randomKek

Reputation: 1128

mongodb database search regex

Each document consists of "name, city and branch" then they also contain "keywords" which is a combines version of "name, city and branch".

I now need a regular expression, this is what I got:

    public function searchLocation($keyword){
        $result = $this->locations->find(array('keywords' => new MongoRegex('/^' . strtolower($keyword) . '/')));

        if($result){
            return $result;
        }

        return false;
    }

Problem is, that when the user input was "grandcaffee london" it won't search for EACH word, or when the user inserts: "Cinema New York".

Thanks for reading.

Upvotes: 0

Views: 247

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

You can do this (matches "grandcaffee" OR "london")

db.locations.find({keywords: {$in: [/grandcaffee/, /london/]}});

or this (matches documents with BOTH "grandcaffee" and "london")

db.locations.find({keywords: {$all: [/grandcaffee/, /london/]}});

Upvotes: 1

Related Questions