Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Regex in new MongoDB library for PHP7/HHVM

Tags:

regex

php

mongodb

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?

like image 776
Alexander Serkin Avatar asked Feb 03 '26 13:02

Alexander Serkin


1 Answers

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

$filter = ['word.word' => new MongoDB\BSON\Regex("hello","i")];
like image 158
Alexander Serkin Avatar answered Feb 05 '26 02:02

Alexander Serkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!