Skip to main content
 首页 » 编程设计

python之MongoDB 和 Meteor 中隐私设置的理想模式设计

2025年05月04日8sxdcgaq8080

我有一组文档,它们周围可以有各种 READ 隐私设置:

  1. 它们可以公开完成(任何注册用户)都可以查看
  2. 只有关注您的人才能看到它们(此“关注者” 数组存储在每个用户的文档中)
  3. 它们也可以对发布文档的人保密。
  4. 他们可以拥有自定义隐私,允许您命名可以查看文档的个人用户。此外,您还可以允许用户组查看文档(例如,可能有一个名为“示例组”的组,其中有 20 个用户。您可以允许该组查看磁贴。)

我不知道如何在 MongoDB 中为此有效地实现一个模式,并且希望深入了解实现这个模式的最佳实践。

请您参考如下方法:

由于您没有指定您正在使用的驱动程序(虽然标记为 Javascript,所以也许您正在使用 Mongoose ?)我将尝试使用伪代码/结构来回答这个问题。

我认为您的 document 集合可能如下所示:

{ 
    _id, 
    title, 
 
    owner, //ref to User collection? 
 
    access,  //'public', 'followers' etc... 
 
    permissions[]  
} 

Permission 可能类似于:

{ 
    // one or the other 
    user_id  
    group_id 
} 

现在,棘手的部分是生成一个给定用户可以查看的文档列表。
解决这个问题

function findDocumentsViewableByUser(userID){ 
 
    var followedBy = //populate a list of userIDs that FOLLOW the passed in userID 
 
    var groupIDs = //populate a list of groupIDs this user is a member of 
 
    // all documents where access = 'public' 
 
    // all documents where access = 'followers' AND owner_id is in followedBy 
 
    // all documents where access = 'custom'  
    // and permissions.user_id = userID OR groupIDs contains permissions.groupID 
} 

根据您的 User 和 Group 类型文档的结构,上述 findDocumentsViewableByUser 中的查询将显着减少。
您最好也为此使用聚合框架。