【发布时间】:2018-11-21 13:35:01
【问题描述】:
我有一个包含类似数据的集合
"Name": "....",
...
"RoomStatusDetails": [
{
"StatusEntryId": ObjectId("5be1895dbb2c5620c4003ae0"),
"RoomId": "41964158-0131-7b55-7781-a29084879e1a",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007765"),
"StartDate": ISODate("2018-11-06T05:00:00.0Z"),
"Notes": "",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Inactive"
},
{
"StatusEntryId": ObjectId("5be1896cbb2c5620c4003ae1"),
"RoomId": "41964158-0131-7b55-7781-a29084879e1a",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007766"),
"StartDate": ISODate("2018-11-06T05:00:00.0Z"),
"Notes": "",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Active"
},
{
"StatusEntryId": ObjectId("5bf4e8a83250041388000edf"),
"RoomId": "d72136ac-b295-d4ea-2d32-0f9ef26b7df1",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007766"),
"StartDate": ISODate("2018-11-26T23:00:00.0Z"),
"Notes": "",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Active"
},
{
"StatusEntryId": ObjectId("5bf4e8db3250041388000ee0"),
"RoomId": "4c980185-65eb-eece-8176-7f187eed3132",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007765"),
"StartDate": ISODate("2018-11-18T23:00:00.0Z"),
"Notes": "",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Active"
},
{
"StatusEntryId": ObjectId("5bf4e97f3250041388000ee1"),
"RoomId": "407225eb-2990-263b-4112-068d3ef0e1b2",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007768"),
"StartDate": ISODate("2018-12-05T23:00:00.0Z"),
"Notes": "",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Active"
}
],
我正在尝试下面的代码行
$this->collection->aggregate(array(
array(
'$project' => array(
'RoomStatusDetails' => array(
'$filter' => array(
'input' => '$RoomStatusDetails',
'as' => 'item',
'cond' => array(
'$or' => [
['$eq' => ['$$item.CurrentStatus', new MongoDB\BSON\ObjectID($this->CAOutOfService)]],
['$eq' => ['$$item.CurrentStatus', new MongoDB\BSON\ObjectID($this->OutOfService)]]
],
['$eq' => ['$$item.IsActive', 'Active']]
)
)
),
'Name' => 1,
'Address' => 1,
'NoOfFloors' => 1,
'Image' => 1,
'Bay' => 1,
'Approved' => 1,
'RoomsDetails' => 1,
'AllotmentsDetails' => 1,
//'TotalNumberOfCapacitiesArray' => 1
)
),
))->toArray();
我正在尝试投影嵌入式文档 RoomStatusDetails,并且我正在尝试仅获取当前状态可以是“5bd17295d2ccda11f0007765”或“5bd17295d2ccda11f0007766”的那些 RoomStatusDetails 数据,它们分别存在于 $this->CAOutOfService 或 $this->OutOfService 变量中并且他们的 IsActive 是“活跃的”。
上面的代码抛出错误“表示表达式的对象必须只有一个字段:{ $or: [ { $eq: [ \"$$item.CurrentStatus\", ObjectId('5bd17295d2ccda11f0007769') ] }, { $eq: [ \"$$item.CurrentStatus\", ObjectId('5bd17295d2ccda11f0007766') ] } ], 0: { $eq: [ \"$$item.IsActive\", \"Active\" ] } } "
请帮忙!!!
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework mongodb-php php-mongodb