3

Trying to figure out how to use Regex in new MongoDB library

I didn't find real world example of usage MongoDB\BSON\Regex so I come up with the code below:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['word' => ['word' => 'heelo']]);
$bulk->insert(['word' => ['word' => 'hello']]);
$manager->executeBulkWrite('db.collection', $bulk);

$filter = ['word' => ['word' => new MongoDB\BSON\Regex("hello","i")]];

$query = new MongoDB\Driver\Query($filter);
$cursor = $manager->executeQuery('db.collection', $query);

foreach ($cursor as $document) {
    var_dump($document);
}

But it shows nothing. Does anyone know how to use it?

1 Answer 1

8

I've found the answer. I should write query like:

$filter = ['word.word' => new MongoDB\BSON\Regex("hello","i")];
Sign up to request clarification or add additional context in comments.

2 Comments

hi, what does "i" stands for ?
"i" is case-insensitive (or ignore case, but same outcome), in most regex libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.