MongoRegex并在集合中搜索多行 [英] MongoRegex and search multiple rows in collection

查看:148
本文介绍了MongoRegex并在集合中搜索多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是以下事情.

$collection = $this->db->products_sale;

        $user_query = preg_replace("/[[:blank:]]+/"," ", $data);
        $arr_query = explode(' ', $user_query);

        if (count($arr_query) > 1) {
        $tmp = array();

        foreach ($arr_query as $q) {
            $tmp[] = new MongoRegex( "/". $q ."/i" );
        }

        $who['keywords'] = array('$in' => $tmp);
        $who['title'] = array('$in' => $tmp);
        $who['description'] = array('$in' => $tmp);

        } else {
            $who['keywords'] = new MongoRegex( "/". $user_query ."/" );
            $who['title'] = new MongoRegex( "/". $user_query ."/" );
            $who['description'] = new MongoRegex( "/". $user_query ."/" );
        }

        print json_encode($who);
        $cursor = $collection->find( $who );

正如您所看到的,我要搜索的内容如下,我需要能够搜索关键字,标题,描述

As you can see I am doing a number of searches, what I would like to do, is the following I need to be able to search keywords, title, description

现在,我可以进行很多搜索,而这些搜索我以为自己正在做,但是显然不能.从我得到的结果来看,它似乎将一个长字符串ARRAY或JSON发送回mongoDB,这与MySQL可以进行OR/AND搜索甚至是MATCH搜索的MySQL不同,它似乎对MongoDB并不那么聪明.

Now I could do a number of searches which is what I thought I was doing, but clearly not. from what I got, it seems it sends one long string ARRAY or JSON back to the mongoDB, unlike MySQL where you can do a OR/AND search and even a MATCH search it seems MongoDB just not that smart.

这是我发现的有效方法,但是随后它搜索了搜索的确切方式,例如

This is what I found works, but then it searches for the exact way the search searches for e.g

将搜索"Windows Phone 7"而不是关键字.

"windows phone 7" would get searched instead of keywords.

$cursor = $collection->find( 
        array('$or' => array(
        array("keywords" => new MongoRegex( "/$user_query/i" )),
        array("product" => new MongoRegex( "/$user_query/i" )),
        array("description" => new MongoRegex( "/$user_query/i" )),
        )));

推荐答案

对于任何想知道我最终如何使它起作用的人.这很简单.我仍在努力,但这是我到目前为止的工作.

For anyone that wants to know how I ended up getting it to work. It was quite simple. I am still working on it, but here is what I have so far.

$collection = $this->db->products_sale;

    $user_query = preg_replace("/[[:blank:]]+/"," ", $data);
    $arr_query = explode(' ', $user_query);

    if (count($arr_query) > 1) {
    $tmp = array();

    foreach ($arr_query as $q) {
        $tmp[] = new MongoRegex( "/". $q ."/i" );
    }

    $who['keywords'] = array('$in' => $tmp);
    $who['product'] = array('$in' => $tmp);

    } else {
        $who['keywords'] = new MongoRegex( "/". $user_query ."/" );
        $who['product'] = new MongoRegex( "/". $user_query ."/" );
    }

    //print json_encode($who);
    $cursor = $collection->find( 
    array('$or' => array($who
    )));


    if ($cursor->count() > 0)
    {
    //print "found you";
    $test = array();
    // iterate through the results
    while( $cursor->hasNext() ) {   
        $test[] = ($cursor->getNext());
    }
    print json_encode($test);

这篇关于MongoRegex并在集合中搜索多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆