aaron p
aaron p

Reputation: 443

is there a way to do a meteor find() based upon more than one field?

Is there a way to do a meteor find (yes, it will be part of a publish function later, i'm just trying to test this) that is filtered by more than one field at a time? for example, let's say I only want to return those users who work at Disney in the IT department.

so far I have

Template.managerReports.disney = function () {  
return employees.find({'company':"disney"});         
};

//for template helper binding to
<template name="managerReports">
{{#each disney}}
 <p>{{name}}</p>
{{/each}}
</template>

and attempts to add multiple comma-separated {field:value} pairs results very nicely in these ALSO being found and added to the disney results. I want to reduce results per added field. only data that matches multiple fields gets found.

am I being totally dumb? what is the usual meteor way to deal with multiple conditional finds?

Upvotes: 0

Views: 89

Answers (2)

Hubert OG
Hubert OG

Reputation: 19544

You need to pass a javascript object, not comma-separated values:

Employees.find({
  company: 'disney',
  department: 'marketing',
  salary: {$gt: 100000},
});

Upvotes: 0

David Weldon
David Weldon

Reputation: 64342

If you want to find users who work for disney AND in the IT department:

employees.find($and: [{company: 'disney'}, {department: 'it'}]);

However, mongodb provides an implicit AND operation when specifying a comma separated list of expressions. So this is more commonly written as:

employees.find({company: 'disney', department: 'it'});

If you want to find users who work for disney OR in the IT department:

employees.find($or: [{company: 'disney'}, {department: 'it'}]);

Upvotes: 1

Related Questions