Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:19:20+00:00 2026-06-13T10:19:20+00:00

Using Meteor, I’m wondering how best to handle different client-side collections that share the

  • 0

Using Meteor, I’m wondering how best to handle different client-side collections that share the same server-side database collection. Consider the following example: I have a User collection, and on my client-side I have a list of users that are friends and I have a search feature that performs a query on the entire users database, returning a list of usernames that match the query.

On the Publish server-side method, I have two queries against the same collection that return different sets of documents. Should this data go into two separate collections on the client-side? Or should all of the User documents that match both queries end up in the same collection? If the latter, would I then duplicate code used for both the server-side and client-side query?

On the server:

Meteor.publish('searchResults', function(query){
  var re = new RegExp(query, 'i')
  return Users.find({ 'name' : {$regex: re}})
})

On the client:

Session.set('searchQuery', null)

Meteor.autosubscribe(function(){
  Meteor.subscribe('searchResults', Session.get('searchQuery'))
})

Template.search.events = {
  'keyup #user-search' : function(e){
    Session.set('searchQuery', e.target.value)
  }
}

_.extend(Template.search, {

  searchResults: function() {
    var re = new RegExp(Session.get('searchQuery'), 'i')
    return Users.find({ 'name' : {$regex: re}})
  }
})

This seems like a plausible solution, but not an optimal one. What if I wanted to create a new client-side collection that consisted of search results from multiple server-side Collections?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:19:21+00:00Added an answer on June 13, 2026 at 10:19 am

    In a shared area:

    function getSearchUsers(query) {
      var re = new RegExp(query, "i");
      return Users.find({name: {$regex: re}});
    }
    
    function getFriendUsers() {
      return Users.find({friend: true});    // or however you want this to work
    }
    

    On the server:

    Meteor.publish("searchUsers", getSearchUsers);
    Meteor.publish("friendUsers", getFriendUsers);
    

    On the client:

    Template.search.onCreated(function () {
       var self = this;
       self.autorun(function () {
         self.subscribe("searchUsers", Session.get("searchQuery"));
       });
    });
    
    Template.friends.onCreated(function () {
      this.subscribe("friendUsers");
    });
    
    Template.search.helpers({
      searchResults: function () {
        return getSearchUsers(Session.get("searchQuery"));
      }
    });
    
    Template.friends.helpers({
      results: function () {
        return getFriendUsers();
      }
    });
    

    The key takeaway from this is that what happens behind the scenes when the data
    is getting transferred over the wire isn’t obvious. Meteor appears to combine
    the records that were matched in the various queries on the server and send this
    down to the client. It’s then up the client to run the same query again to split
    them apart.

    For example, say you have 20 records in a server-side collection. You then have
    two publishes: the first matches 5 records, the second matches 6, of which 2 are
    the same. Meteor will send down 9 records. On the client, you then run the exact
    same queries you performed on the server and you should end up with 5 and 6
    records respectively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using meteor. I'm wondering if theres a shorthand way to do batch updates
Is it possible to directly bind server side events to client side objects in
I'm using wordpress and I used a plugin called meteor slideshows that I believe
I am interested in creating an application using the the Meteor framework that will
I'm using meteor with the bootstrap package. (here is a chunk of code where
Using SQL Server 2008 R2 we are looking for a way to select the
I am using Meteor 4.2 (Windows) and I am always getting the update failed:
I tried replicating the todos application without using the example meteor terminal commentlets Steps,
I just started using meteor today and can't seem to figure out what I'm
Further to my previous post about obtaining data from objects on the server using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.