我有一个 HostelTbl 集合,其中有很多文件,其中一个文件就像
{
"_id": ObjectId("5ae69fb4d2ccda0e70005551"),
"Name": "Hostel-MGL-02",
...
"RoomsDetails": [
{
"RoomId": "9a21e427-16fd-7a9e-bc16-537dc55093bb",
"FloorNumber": "1",
"RoomNumber": "101",
...
},
{
"RoomId": "050c65ab-355e-9bec-00a0-2999e75bcdc4",
"FloorNumber": "1",
"RoomNumber": "104",
...
},
{
"RoomId": "f197c635-a6fb-b4d6-9cf5-ed53177edd47",
"FloorNumber": "2",
"RoomNumber": "109",
...
},
...
{
...
}
],
"AllotmentsDetails": [
{
"AllotmentId": "5eb05e1d-2690-afad-747a-ef29450ae3f3",
"StudentId": ObjectId("5ab8d5d4ff24ae120400085e"),
"RoomId": "9a21e427-16fd-7a9e-bc16-537dc55093bb",
"FromDate": ISODate("2018-06-12T22:00:00.0Z"),
"ToDate": ISODate("2018-07-04T22:00:00.0Z"),
...
},
{
"AllotmentId": "40d73d84-0f94-48d8-823c-2255fd2ae0ed",
"StudentId": ObjectId("5ab8d5bcff24ae120400085d"),
"RoomId": "050c65ab-355e-9bec-00a0-2999e75bcdc4",
"FromDate": ISODate("2018-01-21T22:00:00.0Z"),
"ToDate": ISODate("2018-02-06T22:00:00.0Z"),
...
},
{
"AllotmentId": "40d73564-0f94-48d8-823c-2255fd2440428",
"StudentId": ObjectId("5ab8d5bc4624ae1204000855),
"RoomId": "f197c635-a6fb-b4d6-9cf5-ed53177edd47",
"FromDate": ISODate("2018-03-21T22:00:00.0Z"),
"ToDate": ISODate("2018-05-04T22:00:00.0Z"),
...
}
]
}
我在一个文档中有两个嵌入式文档。一个是存储房间信息,另一个是存储房间分配给学生信息。现在我想从 RoomsDetails 中获取那些可用房间,如果他们的分配详细信息的“ToDate”字段包含从今天开始的过去日期在 AllotmentsDetails Embedded 文档中。与上述情况一样,ID 为“050c65ab-355e-9bec-00a0-2999e75bcdc4”、f197c635-a6fb-b4d6-9cf5-ed53177edd47 的房间在 AllotmentsDetails 嵌入文档中包含过去的 ToDate 日期。它应该只取那两个房间。
我找不到这样的查询。请帮忙!!!
我正在尝试调整以下代码行
public function fetchAllAvailableRooms()
{
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)
),
array(
'$project' => array(
'RoomsDetails' => array(
'$filter' => array(
'input' => '$AllotmentsDetails',
'as' => 'allot',
'cond' => array(
'$lt' => array('$$allot.FromDate', $this->Today)
)
)
),
'Name' => 1
)
)
))->toArray();
return $cursor;
}