4

Performing a query that simulates a 'like/mysql' searching for teams on the name of the team

Team document structure

{
    "_id": 9,
    "name": "azerty",
    "tag": "dsfds",
    "desc": "ggdfgsdfgdfgdf",
    "captain": 8,
    "coach": 8,
    "members": [{
        "date_joined": "2016-03-31 15:22:09",
        "user_id": 8
    }, {
        "date_joined": "2016-03-31 19:22:35",
        "user_id": 9
    }],
    "current_invites": [{
        "invite_id": 21,
        "username": "Nikki",
        "user_id": "9",
        "status": 1,
        "date_invited": "2016-03-31 18:32:40"
    }, {
        "invite_id": 22,
        "username": "Nikki",
        "user_id": "9",
        "status": 2,
        "date_invited": "2016-03-31 18:33:16"
    }]
}

PHP Code =

$q = '/.*'.$q.'*./';
$result = $this->coll->aggregate(
           array('$match' => array('name' => $q)),
           array('$project' => array('name' => 1,'members' => array('$size' => '$members'))));

Feels like I'm going mad not knowing how to fix this. Have used regex before after migrating to mongo but not with the combination of agg-match.

3
  • 1
    This is what you are looking for: stackoverflow.com/questions/16252208/… Commented Mar 31, 2016 at 22:11
  • Found the right post for my situation and using 'new MongoRegex($q)' fixed my issue Commented Mar 31, 2016 at 22:32
  • For those stumbling on making mongo regex with php, options should be separated from regex pattern. Something like ['$match' => ['$regex' => '^' . $pattern . '.*', '$options' => 'i']]. More details stackoverflow.com/questions/16252208/… Commented Feb 19, 2020 at 12:53

1 Answer 1

0

in my case i am finding the aggregated result in which you can not set the where clause so use the aggregated functions like $sort $unwind $orderby and so on i am using the all of the above mention and have the problem with the like stuff to match the string like %str% here my code in which i implement the like using $match with MongoRegex

public function getRecords($table,$where = array(),$like_key = false,$like_value = false,$offset = 1,$limit = 10,$order_column = false,$order_type = false, $isAggregate=false,$pipeline=array()){
    $temp = $this->getMongoDb()->where($where);
    if($like_key && $like_value){
        $temp = $temp->like($like_key,$like_value);
        // this like filter is for aggregated result work both on normal get record or by aggregated result
        $pipeline[]=array(
            '$match' => array( $like_key => new MongoRegex( "/$like_value/i" ) )
        );
    }
    if($order_column && $order_type){
        $order_by = array();
        $order_by[$order_column] = $order_type;
        $temp = $temp->order_by($order_by);

        $pipeline[]=array(
            '$sort'=>array($order_column => ($order_type =="desc")? -1 : 1)
        );
    }

I got the solution when I read the following aggregation framework go the aggregation framework

I hope you will get your solution to resolve your issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.