【问题标题】:PHP mongo find field starts withPHP mongo 查找字段以
【发布时间】:2014-11-10 10:00:38
【问题描述】:

我正在尝试为 php mongo 做类似 mysql 的操作;在我的文章集中找到任何以 www.foo.com/{category} 开头的链接。我可以在 shell 中很好地执行它,但 php 驱动程序似乎没有正确插入我的命令。而且 mongo 正则表达式缺乏详尽的文档。这是我的代码。

$cats = ['news', 'life', 'humor'];

foreach($cats as $cat){
    $category = 'www.foo.com/' . $cat;
    $articles = db()->articles->find(['link' => array('$regex'=>new MongoRegex("/^$category/"))]);
}

它返回文章但链接不匹配。

【问题讨论】:

    标签: php regex mongodb


    【解决方案1】:

    我尝试连接正则表达式,然后将其传递给 mongo 查询,而不是在查询中对其进行正则表达式并删除引号,这似乎有效。希望它可以帮助遇到类似问题的其他人

    $cats = ['news', 'life', 'humor'];
    
    foreach($cats as $cat){
        $prefix = '/^'; 
        $suffix = '/'; // prefix and suffix make up the regex notation for text that starts with 
        $category = $prefix . 'www.foo.com/' . $cat . $suffix;
        $articles = db()->articles->find(['link' => array('$regex'=>new MongoRegex($category))]);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 2019-03-08
      • 2013-02-22
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      相关资源
      最近更新 更多