Search php mongodb '$ or' regex

I am trying to query the mongodb "video" collection to find results by the "title" or "tags" fields ... it keeps returning 0 results even when I search for the terms that I know in the title and / or tags ... any help will be appreciated

<?php $user_query = preg_replace("/[[:blank:]]+/"," ", $_GET['q']); $arr_query = explode(' ', $user_query); foreach ($arr_query as $q) { $title[] = '/'. $q .'/i'; $tags[] = '/'. $q .'/i'; } $who=array( '$or' => array( array('$in' => array('$regex' => $title)), array('$in' => array('$regex' => $tags)) ) ); $vids=$videos->find($who); ?> 
+4
source share
2 answers

You need to specify some fields for $in :

 $who=array('$or' => array( array('somefield' => array('$in' => array(new MongoRegex($title)))), array('otherotherfield' => array('$in' => array(new MongoRegex($tags)))) )); 

So this works, saying, saying: if some field is in the range of some values

http://docs.mongodb.org/manual/reference/operator/in/

Edit

This may still not work due to the built-in $regex . If so, you can try:

 $who=array('$or' => array( array('somefield' => new MongoRegex($title)), array('otherotherfield' => new MongoRegex($tags)) )); 

Edit

If any of these queries do not work, you can:

 $who = array('$or' => array()); foreach($arr_query as $q){ $who['$or'][] = array('title' => new MongoRegex("/^$q/")); $who['$or'][] = array('tags' => new MongoRegex("/^$q/")); } 

Something like this should work, again it is untested, but if my memory serves me correctly, it should do it.

Other editing

This works fine for me:

 $mongo = new Mongo(); $db = $mongo->tstvid; $videos = $db->videos; $videos->insert(array('title' => 'test1', 'tags' => array('h','h'))); $videos->insert(array('title' => 'test2', 'tags' => array('h','h'))); $videos->insert(array('title' => 'test3', 'tags' => array('h','h'))); $videos->insert(array('title' => 'tst3', 'tags' => array('h','test'))); $user_query = preg_replace("/[[:blank:]]+/"," ", "test"); $arr_query = explode(' ', $user_query); if (count($arr_query) > 1) { $who = array( '$or' => array() ); foreach ($arr_query as $q) { $who['$or'][] = array('title' => new MongoRegex("/^". $q ."/i")); $who['$or'][] = array('title' => new MongoRegex("/^". $q ."/i")); } } else { $regex=new MongoRegex("/^". $user_query ."/i"); $tregex=new MongoRegex("/^". $user_query ."/i"); $who=array( '$or' => array( array('title' => $regex), array('tags' => $tregex) ) ); } $vids=$videos->find($who); $results=""; $i=0; foreach($vids as $vid){ $results .= "<li>".$vid['title']."</li>\n"; $i++; } if($i==0){ $results="<em>No results found</em>"; } echo $results; 

And he outputs:

 test1 test2 test3 tst3 

So, I'm not sure what is wrong, but I would recommend double checking that your script is breaking the keywords correctly and the schema is being viewed correctly, calling these requests in the console.

It should be noted that I also tried this:

 $user_query = preg_replace("/[[:blank:]]+/"," ", "test h"); 

And it worked.

0
source
 $search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_GET['q']); $search_string = $search_string; //Connecting mongoDB $dbhost = 'localhost'; $dbname = 'yourdbname'; $port = '27017'; $m = new MongoClient(); $db = $m->$dbname; if($m){ // select a collection $videos = $db->videos; // search for title and tags $searchQuery = array( '$or' => array( array( 'title' => array( '$regex' => $search_string, ), ), array( 'tags' => array( '$regex' => $search_string, ), ), ) ); $cursor = $videos->find($searchQuery); foreach ($cursor as $doc) { var_dump($doc); //or what ever you want //var_dump($doc['title']); } else{ echo "Collection videos cant select :( <br>";die(); } 

Thinks this will help .: D

0
source

All Articles