Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a more complex document "schema" saved in Mongo but the part that I need to match looks like this

"tags" : [
    {
        "tag" : "accompong maroon festival",
        "type" : "label"
    },
    {
        "tag" : "jamaica",
        "type" : "label"
    },
    {
        "tag" : "maroon warrior",
        "type" : "label"
    },
    {
        "tag" : "maroons",
        "type" : "label"
    },
    {
        "tag" : "caribbean culture",
        "type" : "label"
    },
    {
        "tag" : "rum",
        "type" : "label"
    }
}

I am using PHP to query the Mongo database and I have to query each document against an array of possible words.

array(
    'boxing',
    'warrior'
)

I don't know how to write the code in order to try to match the array that I have with the dataset saved in Mongo. For now I only try to see if the tag is within the array of words

$data = $this->event_model->find_by(
            array
            (
                'tags.tag' => array
                (
                    '$in' => $Words
                ),
                'published' => 'y'
            )
        );
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I've resolved this problem by first creating an array that uses MongoRegex to search through the tags and by adding this array to the $or procedure

        $or_array = array();
        foreach($Words as $w)
        {
            $or_array[] = array(
                'tags.tag' => new MongoRegex('/.*'. $w .'.*/i')
            );
        }

        $data = $this->event_model->find_by(
            array
            (
                '$or' => $or_array,
                'published' => 'y'
            )
        );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.