4

我正在尝试查询 mongodb“视频”集合以通过“标题”或“标签”字段查找结果......即使我搜索我知道在标题和/或标签中的术语,它也会不断返回 0 个结果......任何帮助,将不胜感激

<?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

2 回答 2

0
$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();
    }

认为这会有所帮助。:D

于 2014-10-16T06:11:54.577 回答
0

您需要为您指定一些字段$in

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

所以它的工作原理是:如果某个字段在某个值的范围内

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

编辑

由于嵌入式$regex. 如果是这种情况,那么您可以尝试:

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

编辑

如果这些查询中的任何一个都不起作用,您可以执行以下操作:

$who = array('$or' => array());

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

像这样的东西应该可以工作,它再次未经测试,但如果我的记忆对我有帮助,应该这样做。

另一个编辑

这对我来说非常有效:

$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;

它输出:

test1
test2
test3
tst3

所以我不确定出了什么问题,但我建议仔细检查您的脚本是否正确分解了关键字,并且通过在控制台中发出这些查询来正确搜索架构。

应该注意的是,我也尝试过:

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

它也很有效。

于 2012-12-24T17:15:20.067 回答