In Meteor, how can I query only the records of a given subscription?

前端 未结 3 1637
北海茫月
北海茫月 2020-12-18 07:09

I understand that a a subscription is a way to flow records into a client-side collection, from this post, and others...

However, per this post, You can have multipl

相关标签:
3条回答
  • 2020-12-18 07:46

    What you seem to want to do is maintain two separate collections of records, where each collection is populated by a different publication. If you read the DDP specification, you'll see that the server tells the client which collection (not publication) each record belongs to, and multiple publications can actually provide different fields to the same record.

    However, Meteor actually lets you send records to any arbitrary collection name, and the client will see if it has that collection. For example:

    if (Meteor.isServer) {
      Posts = new Mongo.Collection('posts');
    }
    
    if (Meteor.isClient) {
      MyPosts = new MongoCollection('my-posts');
      OtherPosts = new MongoCollection('other-posts');
    }
    
    if (Meteor.isServer) {
      Meteor.publish('my-posts', function() {
        if (!this.userId) throw new Meteor.Error();
    
        Mongo.Collection._publishCursor(Posts.find({
          userId: this.UserId
        }), this, 'my-posts');
    
        this.ready();
      });
    
      Meteor.publish('other-posts', function() {
        Mongo.Collection._publishCursor(Posts.find({
          userId: {
            $ne: this.userId
          }
        }), this, 'other-posts');
    
        this.ready();
      });
    }
    
    if (Meteor.isClient) {
      Meteor.subscribe('my-posts', function() {
        console.log(MyPosts.find().count());
      });
    
      Meteor.subscribe('other-posts', function() {
        console.log(OtherPosts.find().count());
      });
    }
    
    0 讨论(0)
  • 2020-12-18 07:46

    Of course! It just depends on where you write your subscriptions. In a lot of cases you might be using Iron Router, in which case you would have a given route subscribe to just the data that you need. Then from within that route template's helper you can only query documents within that subscription.

    But the general idea is that you hook up a particular subscription to a particular template.

    Template.onePost.helpers({
      post: function() {
        Meteor.subscribe('just-one-post', <id of post>);
        return Posts.findOne();
      }
    });
    
    Template.allPosts.helpers({
      posts: function() {
        Meteor.subscribe('all-posts');
        return Posts.find();
      }
    ));
    
    0 讨论(0)
  • 2020-12-18 08:00

    This is what's happening:

    Say that your server-side BlogPosts Mongo collection contains 500 posts from 10 different users. You then subscribe to two different subscriptions on the client:

    Meteor.subscribe('posts-current-user'); // say that this has 50 documents
    Meteor.subscribe('posts-by-user', someUser); // say that this has 100 documents
    

    Meteor will see Meteor.subscribe('posts-current-user'); and proceed to download the posts of the current user to the client-side Mini-Mongo's BlogPosts collection.

    Meteor will then see Meteor.subscribe('posts-by-user', someUser); and proceed to download the posts of someuser to the client-side Mini-Mongo's BlogPosts collection.

    So now the client-side Mini-Mongo BlogPosts collection has 150 documents, which is a subset of the 500 total documents in the server-side BlogPosts collection.

    So if you did BlogPosts.find().fetch().count in your client (Chrome Console) the result would be 150.

    0 讨论(0)
提交回复
热议问题