\MongoDB\BSON\Regex in aggregation pipeline - Convert regex from Python to PHP
<p>I'm trying to match some input parameters in my API call using the newer Regex library in PHP, but it's not working so far. I'm using string interpolation to achieve this, but its returning any results. Here is my code:</p>

<pre><code>$regex = new \MongoDB\BSON\Regex ("^{$this-&gt;device_id}:", 'i');

$pipeline = [
      [
        '$match' =&gt; [
        '_id' =&gt; $regex,

      ]]
</code></pre>

<p>My document _id are of the type <code>'london_10:2016-10-05 09'</code> which is the <code>device_id:datehour</code>.</p>

<p>When I var_dump the (string)$regex I get the following, which appears to be working:</p>

<pre><code>string(15) "/^london_10:/i"
</code></pre>

<p>The issue is that when adding this to the pipeline, it returns an empty collection. I have equivalent code which is confirmed working but written in Python, and I need to rewrite it in PHP:</p>

<pre><code>    pipeline = [
        {
            '$match': {
                "_id": re.compile("^%s:" %(self.device_id) )
            }
        }
    ]
</code></pre>

<p>The original code in the question is actually correct - I found that the problem wasn't in this part of my pipeline, but further downstream.</p>

php python regex mongodb aggregate