我正在使用mongodb 3.6版本。
我的车辆桌子上有
之类的文件 {
"_id": ObjectId("5b976220d2ccda12fc0050fb"),
"VehicleNumber": "JK 678",
"NumberOfSeats": "47",
}
{
"_id": ObjectId("67976220d2ccda12fc005068"),
"VehicleNumber": "JK 779",
"NumberOfSeats": "47",
}
我有包含类似数据的路由表
{
"_id": ObjectId("5b7fb426d2ccda11fc005185"),
"Name": "New Jersey City",
"VehicleDetails": [
{
"VehicleEntryId": "b8d0d2b5-8f32-6850-4d79-34ed79138d6d",
"VehicleId": ObjectId("5b976220d2ccda12fc0050fb"),
...
"Status": "Active"
},
{
"VehicleEntryId": "b8d0d2b5-8f32-6850-4d79-34ed79138568",
"VehicleId": ObjectId("67976220d2ccda12fc005068"),
...
"Status": "Active"
}
],
...
}
我写了类似的mongodb聚合查询
$cursor = $this->collection->aggregate([
['$match' => ["_id" => new MongoDB\BSON\ObjectID('5b7fb426d2ccda11fc005185')]],
[
'$addFields' => [
'filteredIds' => [
'$map' => [
'input' => '$VehicleDetails',
'as' => 'item',
'in' => [
'$cond' => [
['$eq' => ['$$item.Status', 'Active']],
'$$item.VehicleId',
false
]
]
]
]
]
],
array(
'$lookup' => array(
'from' => 'VehicleTbl',
'$pipeline' => [
[ '$match'=> ["_id" => ['$in' => ['$$filteredIds'] ]]],
],
'as' => 'AllotedVehicleDetails'
)
),
])->toArray();
基本上,我正在尝试将所有分配的车辆提取到特定路线。因此,我首先通过使用$ addFeilds运算符获取所有主动分配的车辆ID,并将它们放入“ filteredIds”中,然后在查找中尝试使用管道从车辆表中获取车辆信息。上面的代码抛出错误消息“未捕获的异常'MongoDB \ Driver \ Exception \ RuntimeException”,并带有消息'$ lookup参数'$ pipeline:[{$ match:{_id:{$ in:[“ $$ filteredIds”]}}} ]”必须为字符串,类型为“ 32行”中的数组。
请帮助!!!
答案 0 :(得分:1)
使用pipeline
代替$pipeline
。
示例:
{
$lookup : {
'from' : 'vehicles',
let : { localFilterIds : '$filteredIds'},
pipeline : [
{'$match' : { $expr: { '$in' : [ '$_id', '$$localFilterIds' ] }} }
],
'as' : 'AllotedVehicleDetails'
}
}
注意:这是在mongoDb
GUI Robo 3T
中进行的测试。