根据用户角色选择集合的文档

时间:2016-05-09 07:38:34

标签: javascript mongodb meteor

我需要获取集合的所有文档,这些集合具有字段(section)的特定值(取决于用户角色)或字段user的userId。

所以我试图检查用户是否在角色中并将其放入查询中:

var sectionOne = Roles.userIsInRole(Meteor.userId(), ['something']) ? { section: 'cars' } : undefined;
var sectionSecond = Roles.userIsInRole(Meteor.userId(), ['anything']) ? { section: 'vegetables' } : undefined;
var sectionThird = Roles.userIsInRole(Meteor.userId(), ['else']) ? { section: 'countries' } : undefined;

var count = Collection.find(
    {
        $or: [
            sectionOne,
            sectionSecond,
            sectionThird,
            { user: userId },
        ],
        read: { 
            $nin: [ userId ] 
        }
    }
).count();

console.log(count);

这样做的正确方法是什么?

为了更好地理解这里我正在尝试做的一个例子:

示例

如果用户具有somethingelse角色,则查询应如下所示:

var count = Collection.find(
    {
        $or: [
            { section: 'cars' },
            { section: 'countries' },
            { user: userId },
        ],
        read: { 
            $nin: [ userId ] 
        }
    }
).count();

所有具有carscountries或userId且userId不在读取数组中的文档都将被选中,对吧?

更新

我认为我的问题不够准确。所以让我更好地解释一下:

我希望使用user: userId获取所有收集文档 - 如果有任何文档与此匹配。

附加(!)如果用户具有section: 'cars'角色,我需要获取与something匹配的所有文档。所以在这种情况下会有更多的结果。

如果用户还有角色anything,我还想获得section: 'vegetables'的结果并将其添加到结果中。因此,所有这些事情都不相互依赖。

排除最后一件事read: { $nin: [ userId ] }:对于上述每一场比赛,这应该是必需的。

0 个答案:

没有答案
相关问题