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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程