MongoDB and Mongoose - Chain Search Query Helpers to Narrow Search Results

Not able to pass this challenge.

Can anyone see what’s wrong?

var queryChain = function(done) {
  var foodToSearch = "burrito";
  Person.find({favoriteFoods:foodToSearch}).sort({name : "desc"}).limit(2).select("-age").exec((err, data) => {
     if(err)
       done(err);
    done(null, data);
  })
};
2 Likes

I got it. When sorting alphabetically, it has to be sorted in “asc” order not “desc” order.

7 Likes

hi, thanks for your post! But a little question about this chanllenge, I don’t know the specific usage about sort() , limit() and select() , they didn’t describe in the chanllenge and I searched for them in the Mogoose doc or JS or SQL, still not knowed exactly meanings about them , could you tell me how to know that? thank you!

2 Likes

This doc is pretty good at explaining those. http://mongoosejs.com/docs/queries.html

sort is self explanatory, you can just pass in different parameters to sort how you want. Limit only grabs whatever parameter you pass in and return only that number of results. select only returns specific entries in this case age. It could be name or gender if you pass in those parameters.

Hope this helps!

1 Like

It’s great for me , I just realized these points involved in MongoDB’s syntax, I will review that. Thanks again!

Hello everyone,

Do not know the answers to these questions…

  1. Will there be journal replay programs in case of incomplete entries (if there is a failure in the middle of one)?

  2. When using replication, can some members use journaling and others not?

Can anyone help me, please? Or should I go towards MongoDB Tutorial?

i don't understand why .select("-age")

i am using .select({age : 0})

2 Likes

You should read some doc https://mongoosejs.com/docs/api.html#query_Query-select

1 Like

@akidox thank you for pointed that out, freecodecamp community really amazing.

2 Likes

Hi there,

For those who still didn’t get the answer. This is what I came up with:
Step 1: find the object- look for what your did in the previous challenges to find an object.
Step 2: sort the name in ascending order. Use an object and it’s corresponding key which you want to search. Tip: ascending order is represented by a 1. Check this link for more info: https://mongoosejs.com/docs/api.html#query_Query-select
Step 3: limit is just a single parameter for how many you want to limit it to.
Step 4: Select also uses an object to hide certain property from the result. Here 0 means false and hides name property, wherein 1 means true and will show age property.
Step 5: The exec function requires 2 parameters and returns your usual callback.

If you still can’t solve it then below you may find my answer. Cheers everybody!

var queryChain = function(done) {
  var foodToSearch = "burrito";
  var jsonObject = {favoriteFoods : foodToSearch};
  Person.find(jsonObject).sort({name: 1}).limit(2).select({age: 0}).exec((err, data) => {
    (err) ? done(err) : done(null, data); 
  })
};
5 Likes

Lol. Sometimes when I click the “I completed this challenge” button I pass and sometimes I don’t. It turns out I was using the key name “field” from the mongoose docs inside sort() instead of the name key in our project.

im running your code only but still it fails

var queryChain = function(done) {
var foodToSearch = “burrito”;
Person.find({ favoriteFoods: foodToSearch})
.sort({ name: 1 })
.limit(2)
.select({ age: 0 })
.exec(function(error, people) {
console.log(people);
});
};

I was facing this issue too, just change {name: ‘asc’} in sort, that worked for me

1 Like