1

I'm implementing a search function for a node app with mongoose. I'm fairly new to node.js and mongoose. I implemented a search function following this answer. It works and gives me the results.

But the problem is it gives an array of whole objects. All I need is titles and IDs. Could anyone please suggest me how to get those.

this is my code

Model.find(
    { $text : { $search : req.params.searchtext }}, 
    {score : { $meta: "textScore" }}
)
.sort({ score : { $meta : 'textScore' } })
.exec(function(err, results) {
    if (err) {
      throw err;
      return res.status(403).send({success: false});
    } else{      
      res.json({success: true, results: results});
    }

});

this is another code where I'm finding a similar list.

Model.find({
        'createdBy.id' : user._id
      },'title _id' ,function(err, result) { #callback};

but I cannot use 'title _id' in the search function since the $meta sort key needs to be there for sort the results.

Community
  • 1
  • 1

1 Answers1

0

I could get the change the output by using mongoose query api.

Model.find(
    { $text : { $search : req.params.searchtext }}, 
     {score : { $meta: "textScore" }}
)
.limit(20)
.select('textScore title _id description')
.sort({ score : { $meta : 'textScore' } })
.exec(function(err, results) {
    if (err) {
      throw err;
      return res.status(403).send({success: false});
    } else{      
      res.json({success: true, results: results});
    }

}); 

all I had to do is just adding select('textScore title _id description') line.