user3323307
user3323307

Reputation: 253

Find users based on array of IDs using Meteor and MongoDB

I'm currently displaying ALL users from Meteor.users.find() on a webpage with {{#each user}}. However, I want to limit my search based on zip codes.

  1. User enters zip code.
  2. Find city name of zip code.
  3. Find all zip codes within that city.
  4. Find all users having ANY of the zip codes in that city.
  5. List all users on page.

    // Step 1: User input (OK)
    input_zip = '1234';
    // Step 2: Get city name for this ZIP (OK)
    var zip_place = Locations.findOne({ zipCode: input_zip })['place'];
    // Step 3: Get all zip codes in that city (OK)
    var zip_codes = Locations.find({ place: zip_place }).fetch();
    // Step 4 (I'm aware this syntax is totally wrong, but you get the idea)
    var zip_users = Meteor.users.find({ profile.zipcode: zip_codes }).fetch();
    
    // Step 5: List users on page with zip codes in that array.
    
    {{#each user in zip_users}}
      <p>bla bla</p>
    {{/each}}
    

What is the most effective way of searching for users with values from an array? Complete code example will be much appreciated. Thank you.

Upvotes: 0

Views: 1029

Answers (1)

khem poudel
khem poudel

Reputation: 587

Yo can use $in operator for this purpose.

Meteor.users.find({ 'profile.zipcode':{$in: zip_codes} }).fetch();

For more details about $in operator go to mongo manual

Upvotes: 2

Related Questions