I'm currently implementing a realtime search function in my app and I've come across some behaviour which I'm confused about.
The background is: I have two subscriptions from the same MongoDB database on my server, named posts
.
The first subscription subscribes to the latest 50 posts, and sends the data to the MiniMongo collection
Posts
.The second subscriptions subscribes to the post matching whatever search is entered by the user, and sends this data to MiniMongo collection
PostsSearch
as per below.// client Posts = new Mongo.Collection('posts'); PostsSearch = new Mongo.Collection('postsSearch'); // server Meteor.publish('postsPub', function(options, search) { return Posts.find(search, options); }); Meteor.publish('postsSearchPub', function(options, search) { var self = this; var subHandle = Posts.find(search, options).observeChanges({ added: function (id, fields) { self.added("postsSearch", id, fields); } }); self.ready(); });
My question is, we know from the docs:
If you pass a name when you create the collection, then you are declaring a persistent collection — one that is stored on the server and seen by all users. Client code and server code can both access the same collection using the same API.
However this isn't the case with PostsSearch
. When a user starts searching on the client, the functionality works perfectly as expected - the correct cursors are sent to the client.
However I do not see a postsSearch
in my MongoDB database and likewise, PostsSearch
isn't populated on any other client other than my own.
How is this happening? What is self.added("postsSearch", id, fields);
appearing to do that's it's able to send cursors down the wire to the client but not to the MongoDB database.