PHP mongo find field starts with

后端 未结 1 1928
悲哀的现实
悲哀的现实 2021-01-25 18:39

I\'m trying to do the equivalent of a mysql like for php mongo; finding any link in my articles collection thats starts with www.foo.com/{category}. I can execute it fine in th

相关标签:
1条回答
  • 2021-01-25 19:02

    i tried concatenating the regex then passing it to the mongo query as opposed to regex'ing it within the query along with removing the quotes and that seemed to work. hope it helps someone else who has come across similar issues

    $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))]);
    }
    
    0 讨论(0)
提交回复
热议问题