hi i have a problom with mongo $aggregate on the php driver
i have this code in 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);
this is working but i only get the $match1
not the $match2
i need to get OR match 1 or match2 depending of the most new document. on find this wil work:
$query_or = array('$or' => array($match1,$match2));
$cursor = $log->find($query_or)->sort(array("DateCreated" => -1))->skip($pageSize*($page-1))->limit($pageSize);
but need the $lookup
(join) and i can not use it on find
can someone help me? Thanks
If you absolutely need to use $lookup, just use aggregate instead of find. It does work and will bring you all the data:
db.your_table.aggregate([
{
$match: {
$or: [
{type:"picture"},
{type:"rate"}
]
}
},
{
$lookup: {
'from' : 'users',
'localField' : 'objId',
'foreignField' : '_id',
'as' :'user'
}
},
{
$lookup: {
'from' : 'pictures',
'localField' : 'objId',
'foreignField' : '_id',
'as': 'pic'
}
}
])