想要在mongodb驱动程序类中执行“ OR”查询

时间:2019-01-15 11:36:13

标签: php mongodb mongodb-query aggregate-functions

我的代码执行“ AND”查询,但我想执行OR查询,但无法弄清楚该如何执行。以下是我的“ AND”查询代码。 $ match执行“ AND”查询。预先谢谢你

<?php

$command = new MongoDB\Driver\Command([
    'aggregate' => 'master_admin',
    'pipeline'  => [
        [
            '$match' =>
                ['full_name' => 'gg', 'username' => 'gg'],
        ],
    ],
    'cursor' => new stdClass,
]);

1 个答案:

答案 0 :(得分:0)

从官方文档中读取内容会类似

https://docs.mongodb.com/manual/reference/operator/query/or/

$command = new MongoDB\Driver\Command([
    'aggregate' => 'master_admin',
    'pipeline'  => [
        [
            '$match' => [
                '$or' => [
                           ['full_name' => ['$eq' => 'gg']], 
                           ['username' => ['$eq' => 'gg']]
                ],
             ],
        ],
    ],
    'cursor' => new stdClass,
]);

我还没有测试过,但是应该按照这些方法工作。在文档的帮助下,您应该能够按照自己的意愿进行操作。