Reputation: 253
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.
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
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