【发布时间】:2018-06-12 09:17:06
【问题描述】:
我写了如下代码行
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)
),
array(
'$project' => array(
'AllotmentsDetails' => array(
'$filter' => array(
'input' => '$AllotmentsDetails',
'as' => 'allot',
'cond' => array(
'$and' => array(
'$lt' => array('$$allot.ToDate', new MongoDB\BSON\UTCDateTime((new DateTime())->getTimestamp() * 1000)),
'$eq' => array('$$allotment.RoomId', $this->RoomId)
)
)
)
),
)
)
))->toArray();
它抛出错误消息“未捕获的异常 'MongoDB\Driver\Exception\RuntimeException' 和消息'表示表达式的对象必须只有一个字段:”
请帮忙!!!
【问题讨论】:
-
在
$filter条件的$and内的$lt和$eq上缺少包装array()。 PHP 认为这是{ "$and": { "$lt": [...], "$eq": [...] } },当你想要{ "$and": [{ "$lt": [...] },{ "$eq": [...] }] }。请记住,任何带有=>的东西都意味着{}是隐含的,没有它就是[]。 -
你能在例子中展示一下吗
-
我试过像 '$and' => array( array('$lt' => array('$$allot.ToDate', new MongoDB\BSON\UTCDateTime((new DateTime() )->getTimestamp() * 1000))), array('$eq' => array('$$allotment.RoomId', $this->RoomId)) ) .........它不起作用
-
请在代码段中显示为答案
标签: php mongodb aggregation-framework mongodb-php