Mongodb\BSON\Regex Php: Perform Like Match

后端 未结 1 350
无人及你
无人及你 2021-01-14 09:58

I see these links:

https://docs.mongodb.com/php-library/current/tutorial/crud/#regular-expressions https://docs.mongodb.com/manual/reference/operator/query/regex/#pe

相关标签:
1条回答
  • 2021-01-14 10:49

    Remove the regex delimiters since they are not used in MongoDB\BSON\Regex:

    $cursor = $collection->find([
        'description' => new MongoDB\BSON\Regex('giov', 'i'),
    
    ]);
    

    The answer is quite evident if you follow your first reference link:

    The following example lists documents in the zips collection where the city name starts with “garden” and the state is Texas:

    <?php
    
    $collection = (new MongoDB\Client)->test->zips;
    
    $cursor = $collection->find([
        'city' => new MongoDB\BSON\Regex('^garden', 'i'),
        'state' => 'TX',
    ]);
    
    foreach ($cursor as $document) {
       printf("%s: %s, %s\n", $document['_id'], $document['city'], $document['state']);
    }
    
    0 讨论(0)
提交回复
热议问题