流星:如何检查项目是否在数组字段中,但在发布中排除该字段? [英] Meteor: How to check if item in array field, but exclude that field in Publish?

查看:31
本文介绍了流星:如何检查项目是否在数组字段中,但在发布中排除该字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的帖子收藏集在《流星发布》中做到这一点:

I'm trying to figure out how to do this in a Meteor Publish for my Posts collection:

  1. 检查sharedBy数组是否包含当前的userId.
    • sharedBy是分享此信息的所有用户的数组.
  1. Check if the sharedBy array contains the current userId.
    • sharedBy is an array of all the users that shared this post.
  • 最好只有客户端集合才能添加此新字段.

推荐答案

以下是我正在研究的解决方案,来自Gadi: https://plus.google.com/u/0/107554020054962631548/posts

Here's a solution I have working, from Gadi: https://plus.google.com/u/0/107554020054962631548/posts

对于大量股份,这似乎是一个非常糟糕的主意,即数千股以上,因为Mongo的最大文档大小为16Meg,因此我认为在SharedBy数组中有100K用户将导致严重的性能问题.按照这篇文章,我将努力使其与参考VS嵌入式方式一起使用:

This appears to be a really bad idea for large #'s of shares i.e. over a few thousand, since Mongo has a maximum document size of 16Meg, so I figure with 100K users in the SharedBy array it will cause major performance issues. I'm going to work on getting it to work with references VS embedded way, as per this post: Meteor, One to Many Relationship & add field only to client side collection in Publish?

以下方法可行,但由于SharedBy用户数量的增加,效率不高:

Below works, but will not be efficient due to the growing array of SharedBy users:

// includes a sharedBy: [userId1, userId2, etc...] field.
Posts = new Meteor.Collection('posts');

Meteor.publish('posts', function() {

    var self = this;
    var handle = Posts.find().observeChanges({
        added: function(id, fields) {
            fields.sharedByMe = _.contains(fields.sharedBy, self.userId);
            delete(fields.sharedBy);
            self.added('posts', id, fields);
        },
        changed: function(id, fields) {
            if (fields.sharedBy) {
                fields.sharedByMe = _.contains(fields.sharedBy, self.userId);
                delete(fields.sharedBy);
            }
            self.changed('posts', id, fields);
        },
        removed: function(id) {
            self.removed('posts', id);
        },
    });

    // Stop observing cursor when client unsubscribes
    self.onStop(function() {
            handle.stop();
    });

    self.ready();

});

这篇关于流星:如何检查项目是否在数组字段中,但在发布中排除该字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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