Php MongoDB按$lookup数组筛选查询结果
MongoDB 3.4 我有项目和项目_权限收藏。project_权限集合包含某些用户对项目的权限。单个用户可以对项目拥有多个不同的权限Php MongoDB按$lookup数组筛选查询结果,php,mongodb,Php,Mongodb,MongoDB 3.4 我有项目和项目_权限收藏。project_权限集合包含某些用户对项目的权限。单个用户可以对项目拥有多个不同的权限 [ '$lookup' => [ 'from' => ProjectPermission::collectionName(), 'localField' => '_id', 'foreignField' =&
[
'$lookup' => [
'from' => ProjectPermission::collectionName(),
'localField' => '_id',
'foreignField' => 'project_id',
'as' => 'project_permissions'
]
],
[
'$project' => [
// ... irrelevant fields here
'permissions' => '$project_permissions'
]
],
这是不进行筛选的项目查询结果的外观:
// other project results
// ... other fields
'permissions' => [
0 => [
'_id' => '5d2873aafa873b2b7c000fad'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '562f6bfc05dfe9570fb6e427'
'permission' => 'read'
'created_at' => 1562932138
'updated_at' => 1562932139
]
1 => [
'_id' => '5d2879fdfa873b2b7c000fbd'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '562f6bfc05dfe9570fb6e427'
'permission' => 'write'
'created_at' => 1562932139
'updated_at' => 1562932140
]
2 => [
'_id' => '5db960b5fa873b1604005e8e'
'project_id' => '56a9e5c5d18cacc72a485839'
'user_id' => '582b30dd1e634e6362e1b504'
'permission' => 'write'
'created_at' => 1572430005
'updated_at' => 1572430005
]
]
我想要实现的是只返回那些请求查询的客户对项目有特定权限的项目,例如write
我尝试的方式是:
pipeline: [
0 => [
'$match' => [
// not related to the problem
]
]
1 => [
'$match' => [
'$and' => [
0 => [
'shared_permissions' => [
'$eq' => true
]
]
1 => [
'$or' => [
0 => [
'project_permissions' => [
'$exists' => true
'$ne' => []
]
]
1 => [
'owner_id' => [
'$ne' => MongoDB\BSON\ObjectId#1
(
[oid] => '582b30dd1e634e6362e1b504'
)
]
]
]
]
]
]
]
2 => [
'$lookup' => [
'from' => 'project_permission'
'localField' => '_id'
'foreignField' => 'project_id'
'as' => 'project_permissions'
]
]
3 => [
'$project' => [
// more not important fields here
'shared_permissions' => 1
'permissions' => [
'$map' => [
'input' => [
'$filter' => [
'input' => '$project_permissions'
'as' => 'project_permission'
'cond' => [
'$and' => [
0 => [
'$eq' => ['$$project_permission.user_id', MongoDB\BSON\ObjectId#1
(
[oid] => '582b30dd1e634e6362e1b504'
)
]
1 => [
'$eq' => ['$$project_permission.permission', 'write']
]
]
]
]
]
'as' => 'project_permission'
'in' => [
'user_id' => '$$project_permission.user_id'
'permission' => '$$project_permission.permission'
]
]
]
]
]
]
对此,我几乎得到了正确的回答:
[
0 => [
'_id' => '56a9e5c5d18cacc72a485839'
'short_id' => 3
'title' => 'Modified title'
'owner_id' => '562f692a05dfe9560fb6e428'
'updated_at' => 1572435428
'owner_name' => 'Borat Sagdiyev'
'shared_permissions' => true
'permissions' => [
0 => [
'user_id' => '582b30dd1e634e6362e1b504'
'permission' => 'write'
]
]
]
1 => []
]
问题是空数组,结果被过滤掉了-如果空数组不在结果中就不会有问题,因为如果我使用分页,那么它会显示两个结果,而不是一个。我们知道,在最坏的情况下,我们只能得到一个空数组数组。
因此,我想实现的是,最后一个示例在不使用空数组的情况下也可以实现分页
注:由于某些结构惯例,不可选择展开
有什么想法吗?要在输入文档中的字段与“已加入”集合中的文档中的字段之间执行相等匹配,$lookup stage具有以下语法:
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
{
$lookup:
{
发件人:,
localField:,
外域:,
作为:
}
}
Uhm。。所以问题不在于$lookup。我想要实现的是通过检查用户是否拥有权限来过滤项目结果。在这种情况下,一个项目可以有多个权限,因为管理员和项目所有者需要查看所有权限。但是,仅对不同项目拥有权限的用户应该能够看到属于他们的项目。