MongoDB聚合查询:过滤未共享给当前用户的关联Asset文档
MongoDB聚合Lookup过滤未授权关联资产
需求说明
用户在自身Asset间创建关联导航功能正常,但查看他人共享的Asset时,需要排除该Asset关联的未授权Asset。授权判断条件:
- Asset的attributes数组中,存在属性文档包含当前用户的userId,且typeOfAccess属于
isOwner/asAuthor/asReader; - 或属性文档的_id存在于Asset的attributes数组中(资产共享时会生成对应属性文档)。
数据结构
Link结构
{ "_id": { "$oid": "63769c377615fe4cdb4995a6" }, "userId": "620920aa9ddac2074a50472f", "toAsset": { "$oid": "63769c117615fe4cdb499515" }, "fromAsset": { "$oid": "63769c067615fe4cdb4994d9" }, "comment": "<p>Linking of Note 0001 to Note 0002.</p>", "createdAt": { "$date": { "$numberLong": "1668717623761" } }, "updatedAt": { "$date": { "$numberLong": "1668717623761" } }, "isEmbedded": false, "isActive": true, "__v": 0 }
Asset结构
{ "_id": { "$oid": "6377a8d834671794449f0dca" }, "userId": "636b73f31527830f7bd7a47e", "folderId": "636b73f31527830f7bd7a482", "title": "Note that hasn't been shared", "note": "<p>Here's a Note that hasn't been shared.</p>", "typeOfAsset": "note", "createdAt": { "$date": { "$numberLong": "1668786392389" } }, "updatedAt": { "$date": { "$numberLong": "1668786392389" } }, "isActive": 3, "meta": [...], "preferences": [...], "sequence": 1, "tags": [], "attributes": [{ "$oid": "6377a8d834671794449f0dc8" }], "__v": 0 }
Attributes结构
{ "_id": { "$oid": "6377a8d834671794449f0dc8" }, "userId": "636b73f31527830f7bd7a47e", "numberOfViews": 2, "isFavourite": false, "isToRead": false, "typeOfAccess": "isOwner", "sharing": { "typeOfShare": "withUsers", "sharedWith": [], "segementsForUrl": [] }, "__v": 0 }
现有聚合代码
const match = { $match: { [args.directionOfLink]: new mongoose.Types.ObjectId(args.assetId) } } const project = { $project: { _id: 0, id: '$_id', userId: 1, [directionOfLink]: 1, comment: 1, createdAt: 1, updatedAt: 1, isActive: 1, score: { $meta: 'searchScore' } } } const lookup = { $lookup: { from: 'assets', localField: directionOfLink, foreignField: '_id', let: { attributesInAsset: '$attributes' }, pipeline: [ { $lookup: { from: 'attributes', as: 'attributes', pipeline: [{ $match: { $expr: { $in: [ '$_id', '$$attributesInAsset' ] // $and: [ // { $eq: [ '$userId', context.body.variables.userId ] }, // { $in: [ '$typeOfAccess', ['isOwner', 'asAuthor', 'asReader'] ] }, // ] } } }] } }, { $project: { _id: 1, userId: 1, folderId: 1, title: 1, typeOfAsset: 1, attributes: 1, createdAt: 1, updatedAt: 1, isActive: 1 } } ], as: directionOfLink } }
解决方案
修改lookup阶段的逻辑,在关联Attributes后添加权限过滤,确保仅返回当前用户有权访问的Asset:
const currentUserId = context.body.variables.userId; const match = { $match: { [args.directionOfLink]: new mongoose.Types.ObjectId(args.assetId) } } const project = { $project: { _id: 0, id: '$_id', userId: 1, [directionOfLink]: 1, comment: 1, createdAt: 1, updatedAt: 1, isActive: 1, score: { $meta: 'searchScore' } } } const lookup = { $lookup: { from: 'assets', localField: directionOfLink, foreignField: '_id', // 传入当前用户ID到管道变量 let: { currentUserId: currentUserId, attributesInAsset: '$attributes' }, pipeline: [ { $lookup: { from: 'attributes', as: 'attributes', pipeline: [{ $match: { $expr: { $in: [ '$_id', '$$attributesInAsset' ] } } }] } }, // 新增:过滤当前用户有权访问的Asset { $match: { $or: [ // 情况1:资产属于当前用户 { userId: '$$currentUserId' }, // 情况2:资产有符合权限的属性文档 { $expr: { $gt: [ { $size: { $filter: { input: '$attributes', cond: { $and: [ { $eq: ['$$this.userId', '$$currentUserId'] }, { $in: ['$$this.typeOfAccess', ['isOwner', 'asAuthor', 'asReader']] } ] } } } }, 0 ] } } ] } }, { $project: { _id: 1, userId: 1, folderId: 1, title: 1, typeOfAsset: 1, attributes: 1, createdAt: 1, updatedAt: 1, isActive: 1 } } ], as: directionOfLink } }
逻辑说明
- 将当前用户ID传入
lookup的let变量,供子管道使用; - 关联Attributes后,通过
$match+$or判断两种授权情况:- 资产的
userId直接等于当前用户ID(用户自己的资产); - 过滤后的Attributes数组长度大于0,说明存在当前用户的有效权限记录;
- 资产的
- 不符合条件的Asset会被排除,最终仅返回用户有权访问的关联资产。
内容的提问来源于stack exchange,提问作者Wayne Smallman
相关产品推荐
相关产品推荐

