下面是我收集rolesandresponsibilities文檔的如何查詢子數組中條目與所有條件匹配的文檔?
[{
"_id": {
"$oid": "58b6c380734d1d48176c9e69"
},
"role": "Admin",
"resource": [
{
"id": "blog",
"permissions": [
"read",
"update",
"delete"
]
},
{
"id": "user",
"permissions": [
"create",
"read",
"update",
"delete"
]
},
{
"id": "journal",
"permissions": [
"create",
"read",
"update",
"delete"
]
}
]
},
{
"_id": {
"$oid": "58b7db3cf36d285019ea175f"
},
"role": "Manager",
"resource": [
{
"id": "blog",
"permissions": [
"read",
"update",
"delete"
]
},
{
"id": "user",
"permissions": [
"create",
"read",
"update",
"delete"
]
},
{
"id": "journal",
"permissions": [
"create",
"read",
"update",
"delete"
]
}
]
}]
我要檢查管理員擁有創建訪問與否。 我寫這樣的查詢
var check = { $and: [ { 'role':req.body.role, }, { 'resource.id':'blog'},{'resource.permissions':'create'} ] }
Rolesandresponsibilitie.find(check).populate('user', 'displayName').exec(function(err, rolesandresponsibilities) {
if (err) return next(err);
if (! rolesandresponsibilities) return res.status(404).send('Not Found'); };
return res.status(200).send('Found'); }
});
我越來越不確定
我的意料:
例如1:
角色=管理
ID =博客
權限=創建
我應該得到響應未找到,因爲管理員只
「權限」: 「讀」, 「更新」, 「刪除」 ]
上述3權限
示例2:
role =管理
ID =博客
權限=讀
我應該得到響應發現,由於管理員具有讀取權限
感謝您的幫助 –