0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. This works if i create an array manually like myArray = ['1234', '5678']. var zip_codes = Locations.find({ place: zip_place }, {fields: {'zipCode':1}}).fetch(); creates an array of objects, not an array of zipcodes. How can i get it to create an array of plain zipcodes as strings? ['zipCodes'] at the end before fetch is not working like it does with findOne. Thank you.
I fixed it using .map function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.