Mongodb test don't pass

Tell us what’s happening:
I’ve tweaked the code a lot but as much as I try the tests don’t pass. Appreciate any hint. Thanks

Your project link(s)

solution: https://replit.com/@alirezaghey/mongo-mongoose-fcc-exercies

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.154

Challenge: Chain Search Query Helpers to Narrow Search Results

Link to the challenge:

Hello there,

You appear to have misread this:

…and hide their age.

Hope this helps

Hi,
I’ve corrected the select parameter and still doesn’t work.
Here’s my updated solution.
I’ve done some debugging and it appears that find doesn’t return any records at all. data is always an empty array. I’m suspecting there might be some other problem.
Could you please test it on your end to see whether the challenge accepts any solutions at all?

Thanks!

Appologies, if you have already tried this, but what about without the [] array notation around foodToSearch?

Thanks, that was it. I hadn’t tried this before. What confused me and still isn’t very logical to me is that I used below code in another challenge from this series with the array notation used in the mongodb selector object and it worked perfectly:

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

The above code uses mongo’s Model.findOne.
Below code from this challenge, uses mongo’s Model.find() which also takes a mongodb selector object as the first argument to filter documents.

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

I’m not sure where this, at least on my part perceived, inconsistency comes from.

Anyway, thank you for taking the time and have a nice day.

Well done, on getting through.

I am not sure why it works that way. However, it is just a Mongoosejs thing, and would never work for MongoDB. So, when in doubt, avoid writing property: [val] (unless you specifically need to)

The only bit of literature I could find on the matter is: Mongoose v5.12.12: Mongoose Tutorials: Query Casting

1 Like

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