dsj0312 2012-01-01 17:58
浏览 44
已采纳

MongoDB在PHP中多个$和运算符查询

I’m having trouble with this MongoDB query using PHP array syntax. This is a direct version of the query I want to use.

db.collection.find({
        $or: [
            {$and : [{X:1}, {X: {$gt: 100}}]},
            {$and : [{X:2}, {X: {$lt: 100}}]}
        ]
});

Note: The real query is more complicated, this is just an example.

I wasn’t able to find some examples describing this kind of query in PHP. The best I’ve come up was this:

$query = array(
    '$or' => array(
        array(
            '$and' => array(
                array('X' => 1),
                array('X' => array('gt' => 100))
            )
        ),
        array(
            '$and' => array(
                array('X' => 2),
                array('X' => array('lt' => 100))
            )
        ),
    )
);

$this->db->collection->find($query);

But this query doesn't return any results. Obviously we can't remove $and from the array because we can't have duplicate keys in PHP array.

I don't want to use JavaScript expressions because the speed is critical.

UPDATE: As Alexander Azarov pointed out in comments, my original query can be written differently. I've updated the question with the properly used $and query.

  • 写回答

1条回答 默认 最新

  • doulan2827 2012-01-02 11:10
    关注

    Here is an example of the code with or and and operators in PHP for a Mongo query.

    It uses different field names from what you are going to find in your query, but it is straightforward to understand how to use it.

    Right now I don't have time to change it, to match your query, but if you have problems with that, let me know and I will adjust it. This query is working and finding what it should find.

    $arrFind = array(
        '$or' => array(
            array(
                '$and' => array(
                    array(
                        UI_name => array(
                            '$regex' => 'andrew',
                            '$options' => 'i'
                        )
                    ),
                    array(
                        UI_surname => array(
                            '$regex' => 'mik',
                            '$options' => 'i'
                        )
                    )
                )
            ),
            array(
                '$and' => array(
                    array(
                        UI_surname => array(
                            '$regex' => 'andrew',
                            '$options' => 'i'
                        )
                    ),
                    array(
                        UI_name => array(
                            '$regex' => 'mik',
                            '$options' => 'i'
                        )
                    )
                )
            ),
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?