mongodb与PHP驱动程序聚合(查找)与多个$ match $或$和条件

时间:2017-11-26 08:19:52

标签: php mongodb

嗨,我在php驱动程序上有一个mongo $ aggregate的问题 我在PHP中有这个代码:

$match1 = array( '$and' => array(
    array("uid" => array('$nin'=> $where_blocked)),
    array("type" => "picture" ),
    array("active" => true )
));

$match2 = array( '$and' => array(
    array("uid" => array('$in'=> $where)),
    array("type" => "users" )
));

$query = array(
    array(
        '$match' => array(
            '$or' => array( $match1,$match2 )
        ),
    ),
    array(
        '$lookup' => array(
            'from' => 'users',
            'localField' => 'objId',
            'foreignField' => '_id',
            'as' => 'user'
        ),
    ),
    array(
        '$lookup' => array(
            'from' => 'pictures',
            'localField' => 'objId',
            'foreignField' => '_id',
            'as' => 'pic'
        ),
    ),

    array(
        '$limit' => $pageSize
    ),
    array(  
        '$sort' => array("DateCreated" => -1 )
    ),
    array(
        '$skip' => $pageSize*($page-1)
    )
);

$cursor = $log->aggregate($query);

这是有效的,但我只得到$match1而不是$match2我需要得到OR匹配1或匹配2,具体取决于最新的文档。 找到这个工作:

$query_or = array('$or' => array($match1,$match2));
$cursor = $log->find($query_or)->sort(array("DateCreated" => -1))->skip($pageSize*($page-1))->limit($pageSize);

但需要$lookup(加入),我无法在find上使用它 有人能帮我吗? 感谢

0 个答案:

没有答案