0

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.

12
  • 1
    are you requiring the model in your server.js file? Commented Oct 19, 2015 at 23:10
  • 1
    Just for notes, the whole query is much more clearly written using $in and also without the $and as 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 using mongoose, so be sure your schema definitions match the types required ( all should be string ). Commented Oct 19, 2015 at 23:16
  • I will definitely look into $in, thanks! Using $and is necessary when you use the $or operator more than once per the mongodb docs docs.mongodb.org/manual/reference/operator/query/and Commented Oct 19, 2015 at 23:23
  • Yes, I have this at the top: Breed = require('./models/breed'); Commented Oct 19, 2015 at 23:24
  • 1
    Actually I did say excatly to check your schema that all types were in fact String. The form of $in basically starts like this: { "size": { "$in": [ "s", "m" ] }, "excercise": { "$in": [ "two", "three" ] } } and so on where each field is an implicit "and" and the $in is effectively a shortened $or. Commented Oct 20, 2015 at 0:22

1 Answer 1

1

Are your requiring your Breed model in server.js?

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

1 Comment

Thanks for the reminder to check on the breed model! I had updated the value type in the database from number to string, but not in the breed.js file.

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.