Search code examples
node.jsmongodbexpressmongoosemeanjs

Mongoose Middleware: Keyword with population


I'm using Mongoose Middleware in my MEAN.JS application for searching.

I want to search a word with Moongose Middleware keyword option for the fields: tags.text, description and spot.name

filters = {
   filters : {
     mandatory : {
           exact : { 
             municipality: mongoose.Types.ObjectId(req.user.municipality.id)
             }
      },
     keyword : { 
           fields: ['tags.text','description','spot.name'],
           term:word
           }
      }
 };

For the fields tags.text and description is working.

But the field "spot" is a ObjectID, not a document field, and I'm doing a population, and it's working, but don't works the field spot.name in keyword option...

Any suggestion?

Training
    .find()
    .sort('-created')
    .populate({path: 'spot', select: 'name image likes location'})
    .populate({path: 'user', select: 'displayName imageProfile'})
    .populate('municipality','name')
    .populate('country','name')
    .field(options)
    .filter(filters)
    .keyword(filters)
    .page(pagination,function(err, trainings){
        if (err) {
            console.log(err);
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(trainings);


        }
    });

Solution

  • Maybe isn't a elegant solution, but the only that I found for the moment.

    First I searched the Spot with the word, and then, searched the Training using a filter like this:

    exact:{spot: spotThatIFoundId}