【发布时间】:2015-01-22 05:08:59
【问题描述】:
我说的是:
Meteor.users.findOne() =
{
_id: "..."
...
followers: {
users: Array[], // ["someUserId1", "someUserId2"]
pages: Array[] // ["somePageId1", "somePageId2"]
}
}
对比
Followings.findOne() =
{
_id: "..."
followeeId: "..."
followeeType: "user"
followerId: "..."
}
我发现第二个完全没有效率,因为我需要使用 smartPublish 来发布用户的关注者。
Meteor.smartPublish('userFollowers', function(userId) {
var coursors = [],
followings = Followings.find({followeeId: userId});
followings.forEach(function(following) {
coursors.push(Meteor.users.find({_id: following.followerId}));
});
return coursors;
});
而且我无法过滤 Iron-router 中的用户。我缓存订阅,所以用户可能比我需要的多。
我想做这样的事情:
data: function() {
return {
users: Meteor.users.find({_id: {$in: Meteor.user().followers.users}})
};
},
在 Document 中使用嵌套数组的一个不好的地方是,如果我向 follower.users[] 添加了一个项目,整个数组将被发送回客户端。
那你怎么看?将这些数据保存在用户文档中会更好吗?可能是解决此类问题的“流星方式”。
【问题讨论】: