3

Using MongoDB with PHP's MongoDB driver I am not able to filter search results with regular expressions. In the manual there are no examples given how to use the "filter" option: MongoDB\Driver\Query.

 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
 $filter = array(?????);
 $options = array("projection" => array("fieldname" => 1));
 $query = new MongoDB\Driver\Query($filter, $options);
 $cursor = $manager->executeQuery("dbname.collectionname", $query);
 foreach($cursor as $document) {
    var_dump($document);
 }

I tried about 20 different possibilities but can't find the answer. Queries without regular expressions work fine.

Michael Käfer
  • 1,597
  • 2
  • 19
  • 37

2 Answers2

5

I am stupid. This:

'fieldname' => array('$regex' => 'm')

will find all documents with an "m" somewhere in the field "fieldname".

Michael Käfer
  • 1,597
  • 2
  • 19
  • 37
  • Is that what you wanted or do you still have some sort of question? – ArSeN Jan 10 '16 at 20:11
  • @ArSeN That's it, thank you! Should I delete the whole thing or leave it for others? – Michael Käfer Jan 11 '16 at 21:42
  • I think you should delete it, if you feel that it will not really help other people (e.g. because it is in the docs already). – ArSeN Jan 12 '16 at 16:08
  • @ArSeN I will delete it, before that, can you post the link to the docs? Thank you! – Michael Käfer Jan 13 '16 at 16:52
  • I do not have the link, sorry, I may have phrased that unprecise. I meant if you find that it is actually clear by reading the docs, or something like that. – ArSeN Jan 13 '16 at 17:20
  • @ArSeN For me not, I had to mix different examples of different websites other than the docs, in the docs I didn't find any answer to this. But I guess, for someone who used the old PHP driver for MongoDB this wouldn't have been any problem. It could help people who are really new to this I guess... So I would prefer to leave it but I'm really not sure... – Michael Käfer Jan 13 '16 at 18:31
  • 1
    @MichaelKäfer Please do not delete it, I had the same problem and currently this is the second result in google and the first, that answers the question, how to use regex with this mongoDB-driver. – n.r. Oct 20 '17 at 09:27
3

I was struggeling with the same problem, after moving from the deprecated mongo-driver to the new mongodb-driver. The accepted answer is good, so far. But this additional information may be helpful, too:

The old driver (see php.net) requires the complete regular expression, including slashes, like this:

$query1 = [ 'field' => new MongoRegex("/d+/i") ];

The new driver needs the slashes removed. Also, there are two ways to submit regular expressions, see mongodb-driver-board:

$query1 = [ 'field' => [ '$regex': => '\d+' ]];
$query2 = [ 'field' => new MongoDB\BSON\Regex('\d+'), 'i'];

Note that common reg-ex-flags are coming as the second parameter. Of course, you are free to leave, them. As I did in the first line. By the way, at the end, it looks like both ways are translated to the same:

{"field":{"$regex":"\d+","$options":"i"}}

If you want to keep it dynamic, because you don't know if it's a search string or a regular expression, here is a code example:

if(@preg_match($value, null) !== false){

     $value = new MongoDB\BSON\Regex(trim($value, '/'), 'i');
     // alternatively you may use this:
     // $value = array('$regex' => trim($value, '/'));
     // with options, it looks like this:
     // $value = array('$regex' => trim($value, '/'), '$options' => );''
}
$search = array($field => $value);
$options = [
     "skip" => 0,
     "limit" => 10,
     "projection" => NULL
];

$query = new MongoDB\Driver\Query($search, $options);
$cursor = $this->mongo->executeQuery($database.'.'.$collection, $query);
}
n.r.
  • 831
  • 1
  • 11
  • 30