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.

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

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了