我收藏了 HostelTbl 之类的
{
"_id": ObjectId("5ae845b3d2ccda137000595d"),
"Name": "abc",
"Address": "gggswwrerghyjh",
"NoOfFloors": NumberInt(4),
"Approved": "Yes",
"SchoolId": ObjectId("5a8e9025ff24ae113c005d42"),
"RoomsDetails": [
{
"RoomId": "80a1761f-f8ee-a78f-c6ab-6f9bfbdb8ea3",
"FloorNumber": "3",
"RoomNumber": "5",
"RoomType": ObjectId("5ae8267ed2ccda137000595b"),
"NumberOfBeds": "4"
},
{
"RoomId": "56a1761f-f8ee-a78f-c6ab-6f9bfbdb8es3",
"FloorNumber": "3",
"RoomNumber": "4",
"RoomType": ObjectId("5ae8267ed2ccda137000595b"),
"NumberOfBeds": "5"
}
]
}
请注意,它包含嵌入式文档 RoomsDetails。现在我只想根据 RoomId 获取一个特定的嵌入文档
我努力了
public function fetchRoomById()
{
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)),
)
),
array(
'$project' => array(
'RoomsDetails' => array(
'$filter' => array(
'input' => '$RoomsDetails',
'as' => 'Rooms',
'cond' => array(
'$eq' => array('$$Rooms.RoomId', $this->RoomId)
)
)
),
)
)
);
return $cursor->toArray();
}
它不返回匹配的嵌入文档,它返回主文档而不是嵌入文档。
请帮忙