duananyantan04633 2015-11-09 23:07
浏览 65
已采纳

使用PHP在Mongo中复杂的$和$或查询

I'm trying to execute a mongo find with php using the $and operator combined with the $or operator, however I cant seem to figure out the way to execute it. What this Query is trying to accomplish is search for folder=>INBOX and match the email query for either recipient or from. Here's how I am trying to do it:

Query:

$query['$and'][]['$or'][]['to']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['$or'][]['from']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['$or'][]['recipient']['$regex'] = new MongoRegex("/".$_GET['term'].".*/i");
$query['$and'][]['folder'] = $_GET['folder'];

Print Pre:

Array
(
[$and] => Array
    (
        [0] => Array
            (
                [$or] => Array
                    (
                        [0] => Array
                            (
                                [to] => Array
                                    (
                                        [$regex] => MongoRegex Object
                                            (
                                                [regex] => example@example.com.*
                                                [flags] => i
                                            )
                                    )
                            )
                    )
            )
        [1] => Array
            (
                [$or] => Array
                    (
                        [0] => Array
                            (
                                [from] => Array
                                    (
                                        [$regex] => MongoRegex Object
                                            (
                                                [regex] => example@example.com.*
                                                [flags] => i
                                            )
                                    )
                            )
                    )
            )
        [2] => Array
            (
                [$or] => Array
                    (
                        [0] => Array
                            (
                                [recipient] => Array
                                    (
                                        [$regex] => MongoRegex Object
                                            (
                                                [regex] => example@example.com.*
                                                [flags] => i
                                            )
                                    )
                            )
                    )
            )
        [3] => Array
            (
                [folder] => INBOX
            )
    )
)
  • 写回答

1条回答 默认 最新

  • drc4925 2015-11-09 23:53
    关注

    There is no $and condition involved. All you want to do is search each field with a supplied regular expression to see if it matches content in one of them. This is what an $or operation does, and matches based on "either" condition supplied in an array of arguments as queries:

    {
        "folder": "INBOX",
        "$or": [
            { "to": /example@example.com.*/i },
            { "from": /example@example.com.*/i },
            { "recipient": /example@example.com.*/i }
        ]
    }
    

    Notated for PHP:

    $query = array(
        'folder' => 'INBOX',
        '$or' => array(
            array( 'to' => new MongoRegex("/".$_GET['term'].".*/i") ),
            array( 'from' => new MongoRegex("/".$_GET['term'].".*/i") ),
            array( 'recipient' => new MongoRegex("/".$_GET['term'].".*/i") )
        )
    )
    

    And that is all there is to it.

    All MongoDB query arguments are implicitly an "and" operation anyway, so there is very rarely any need to use the explicit operator. If it were used, then it would be the "wrapping" statement to both conditions:

    {
        "$and": [ 
            { "folder": "INBOX" },
            { "$or": [
                { "to": /example@example.com.*/i },
                { "from": /example@example.com.*/i },
                { "recipient": /example@example.com.*/i }
            ]}
        ]
    }
    

    But you don't need to do that since it is already treated like this anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?