MongoDB & Mongoose Query Chain fails test

Hello! I am doing the challenge Chain Search Query Helpers to Narrow Search Results in the Back End Development and APIs section of the curriculum.

Modify the queryChain function to find people who like the food specified by the variable named foodToSearch . Sort them by name , limit the results to two documents, and hide their age. Chain .find() , .sort() , .limit() , .select() , and then .exec() . Pass the done(err, data) callback to exec() .

My code:

const queryChain = (done) => {
  const foodToSearch = "burrito";
  Person.find({favoriteFoods: [foodToSearch]})
  .sort({name: 1})
  .select({age: 0})
  .exec((err, data)=>{
    done(null, data);

I can’t for the life of me figure out why it’s not working. I would love some help, and thank you so much! :slight_smile:

Remove the brackets from [foodToSearch] in your find method object. It should just be the string value saved to the variable foodToSearch (i.e. "burrito").

Looking at the Schema that might seem a little confusing seeing as favoriteFoods is an array of strings but you are just looking for the value inside the array and not an array with the string value inside it.

But it does make sense that you can’t compare an array with another array as they are separate objects in memory, i.e. [1,2,3] === [1,2,3] is false.

I think mongoose might be doing some casting as well according to the docs but I really am not an expert on how mongoose does things behind the scenes.

1 Like

Thank you, that totally worked! What confuses me though is that in an earlier challenge called Use model.findOne() to Return a Single Matching Document from Your DatabasePassed, I used the following code:

const findOneByFood = (food, done) => {
  Person.findOne({favoriteFoods: [food]}, (err, data)=> {
    if(err) console.error(err);
    done(null, data);

And this passed even though I put brackets around the food parameter. Do you have any insight as to why? Is it because food is a parameter and isn’t a variable or something?

I’m not totally sure.

One guess is that it’s because for the findOne challenge there is only a single item inside the array. So I guess you can do the array compare inside a mongoose find method but then the array content must match exactly.

Chain Search Query Helpers to Narrow Search Results: tests

Post data:

data: JSON.stringify([
  { name: 'Pablo', age: 26, favoriteFoods: ['burrito', 'hot-dog'] },
  { name: 'Bob', age: 23, favoriteFoods: ['pizza', 'nachos'] },
  { name: 'Ashley', age: 32, favoriteFoods: ['steak', 'burrito'] },
  { name: 'Mario', age: 51, favoriteFoods: ['burrito', 'prosciutto'] }

So for { favoriteFoods: [foodToSearch] }

[foodToSearch] is ['burrito'] and not ['burrito', 'hot-dog'] or ['steak', 'burrito'] or ['burrito', 'prosciutto']. So it doesn’t find the content you are looking for and returns an empty array.

Use model.findOne() to Return a Single Matching Document from Your Database: tests

Post data:

$.post(getUserInput('url') + '/_api/find-one-by-food', {
  name: 'Gary',
  age: 46,
  favoriteFoods: ['chicken salad']

Here { favoriteFoods: [food] } => [food] is ['chicken salad']

I might be wrong but that is at least one possibility.

That definitely makes sense if that’s the reason why. Thank you for your clear explanation!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.