Vous devez spécifier certains champs pour votre $in
:
$who=array('$or' => array(
array('somefield' => array('$in' => array(new MongoRegex($title)))),
array('otherotherfield' => array('$in' => array(new MongoRegex($tags))))
));
Cela fonctionne donc en disant :si un champ est dans une plage de certaines valeurs
http://docs.mongodb.org/manual/reference/operator/in/
Modifier
Cela peut toujours ne pas fonctionner à cause du $regex
intégré . Si tel est le cas, vous pouvez essayer :
$who=array('$or' => array(
array('somefield' => new MongoRegex($title)),
array('otherotherfield' => new MongoRegex($tags))
));
Modifier
Si l'une de ces requêtes ne fonctionne pas, vous pouvez :
$who = array('$or' => array());
foreach($arr_query as $q){
$who['$or'][] = array('title' => new MongoRegex("/^$q/"));
$who['$or'][] = array('tags' => new MongoRegex("/^$q/"));
}
Quelque chose comme ça devrait fonctionner, encore une fois, il n'a pas été testé, mais si ma mémoire est bonne, cela devrait le faire.
Une autre modification
Cela fonctionne parfaitement pour moi :
$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;
Et il affiche :
test1
test2
test3
tst3
Je ne sais donc pas ce qui ne va pas, mais je recommanderais de vérifier que votre script décompose correctement les mots-clés et que le schéma est correctement recherché en émettant également ces requêtes dans la console.
Il convient de noter que j'ai également essayé ceci avec :
$user_query = preg_replace("/[[:blank:]]+/"," ", "test h");
Et cela a également fonctionné.