【发布时间】:2018-09-26 06:28:41
【问题描述】:
我在 mongo db 中有一个学生表,例如
{
"_id": ObjectId("5baa85f61d7859401000002a"),
"Name": "Bella Dave",
"RollNo": 12,
"Class": "Ist",
"TransportDetails": [
{
"RouteId": ObjectId("5baa93a21d7859401000002b"),
"StopId": "abc123",
"Status": "Inactive"
},
{
"RouteId": ObjectId("5baa818d1d78594010000029"),
"StopId": "abc456",
"Status": "Active"
}
]
}
我有类似的路由表
{
"Name": "New york City",
"StopDetails": [
{
"StopId": "abc123",
"Name": "Block no 1"
},
{
"StopId": "abc567",
"Name": "Block no 2"
}
]
我在下面写了聚合查询
$cursor = $this->db->TblStudent->aggregate([
[
'$addFields' => [
'ActiveRouteId' => [
'$map' => [
'input' => '$TransportDetails',
'as' => 'item',
'in' => [
'$cond' => [
['$eq' => ['$$item.Status', "Active"]],
'$$item.RouteId',
false
]
]
]
]
]
],
[
'$addFields' => [
'ActiveStopId' => [
'$map' => [
'input' => '$TransportDetails',
'as' => 'item',
'in' => [
'$cond' => [
['$eq' => ['$$item.Status', "Active"]],
'$$item.StopId',
false
]
]
]
]
]
],
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveRouteId',
'foreignField' => '_id',
'as' => 'RouteDetails'
)
),
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveStopId',
'foreignField' => 'StopDetails.StopId',
'as' => 'StopDetails'
)
),
])->toArray();
return $cursor;
基本上,我必须获取活动的路线和站点信息以及学生数据。因此,我使用 $addFields 和 $map 运算符成功获取了 ActiveRouteId 和 ActiveStopId。基于 ActiveRouteId,我正在执行 $lookup 以获取活动路线信息。我在“RouteDetails”嵌入式文档中成功获得了它。现在我的问题是在行
array(
'$lookup' => array(
'from' => 'TblRoute',
'localField' => 'ActiveStopId',
'foreignField' => 'StopDetails.StopId',
'as' => 'StopDetails'
)
),
此查找未获取任何内容。请帮忙!!!
是否可以同时查找路线和站点信息。我的意思是在路由表中还有许多其他嵌入文档,如果可以获取所需的嵌入文档,例如
RouteDetails: [
"Name": "New york City",
"StopDetails": [
{
"StopId": "abc123",
"Name": "Block no 1"
}
]
【问题讨论】:
-
你的mongodb版本是多少?
-
是3.6版本...请帮忙!!!
-
@ninda 请稍候。
-
没有数据符合条件 { "$match": { "$expr": { "$in": ["$StopDetails.StopId", "$$activeStopId"] }} }, ],
-
再次感谢您的帮助!!!
标签: mongodb mongodb-query aggregation-framework mongodb-php php-mongodb