I have the following query in my server.js file:
Breed.find({$and: [
{$or: [ { size: 's' }, { size: 'm' } ]},
{$or: [ { exercise: 'two' }, { exercise: 'three' } ]},
{$or: [ { training: 'two' }, { training: 'three' } ]},
{$or: [ { grooming: 'two' }, { grooming: 'three' } ]},
{$or: [ { catfriendly: 'two' }, { catfriendly: 'three' } ]},
{$or: [ { protective: 'two' }, { protective: 'three' } ]},
{$or: [ { affection: 'three' }, { affection: 'four' } ]}
]}, function (err, matches) {
console.log('these are the matches: ' + matches);
res.json(matches);
});
It comes back as undefined. But I've run this query in the Mongo terminal:
db.breeds.find({$and: [ {$or: [ { size: 's' }, { size: 'm' } ]},{$or: [ { exercise: 'two' }, { exercise: 'three' } ]}, {$or: [ { training: 'two' }, { training: 'three' } ]},{$or: [ { grooming: 'two' }, { grooming: 'three' } ]},{$or: [ { catfriendly: 'two' }, { catfriendly: 'three' } ]},{$or: [ { protective: 'two' }, { protective: 'three' } ]},{$or: [ { affection: 'three' }, { affection: 'four' } ]}]});
And it returns 4 breeds that match the query. What am I missing in my server.js file that is making it come back undefined? I tried console logging typeof matches and it said undefined as well.
$inand also without the$andas all MongoDB query arguments are implicitly an "and" condition anyway. If you are still unsure then provide the document(s) you expect to match in your question. It also looks like you are usingmongoose, so be sure your schema definitions match the types required ( all should be string ).String. The form of$inbasically starts like this:{ "size": { "$in": [ "s", "m" ] }, "excercise": { "$in": [ "two", "three" ] } }and so on where each field is an implicit "and" and the$inis effectively a shortened$or.