流星发布和MongoDB [英] Meteor Publish and MongoDB

查看:59
本文介绍了流星发布和MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Meteor发布和MongoDB,我正在尝试决定或找出哪种方法是最佳实践.

I'm trying to decide or find out which option is the best practices when it comes to Meteor publish and MongoDB.

我有一个拥有300多个用户的组织,我的第一个选择是将userId数组添加到组织mongo文档中,然后执行以下操作:

I have an organization that will have over 300 users, my first option is just adding a userId array to the organization mongo document and doing the following:

Meteor.publish('organizationsUsers', function() {
    var organization = Organizations.findOne({_id: this.userId});.fetch();
    var usersArray = _.flatten(_.pluck(organization, "users"), true);
    return Meteor.users.find({_id: {$in: usersArray}}); 
});

我的第二个选择是向每个用户添加一个OrganizationId并执行以下操作:

My second option is just adding an organizationId to each user and doing the following:

Meteor.publish('organizationsUsers', function() {
    var user = Meteor.users.findOne(this.userId);
     return Meteor.users.find({organizationId: user.organizationId});
});

使用第一个选项,我的MongoDB具有长数组,而使用第二个选项,它更简单.

With the first option I have MongoDB with a long array and with the second option its simpler.

推荐答案

在进行一些快速研究并询问一般规则后,请确定一下,如果您的领域是100个项目以上(在我的情况下将超过300个),则最好与第二种方法.

Ok some after some quick research and asking around general rule if your field is more the 100 items which in my case would be over 300 it's better to go with the second approach.

此外,当mongo使用第一个选项查询属于组织的所有用户时,mongo会获取整个数组并通过索引查找每个用户,而第二个选项仅通过索引查找.

Also when mongo queries all the users belonging to an organization with the first option mongo takes the entire array and looks each via the index, with the second option just via the index.

这篇关于流星发布和MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆