我在我的MEAN.JS应用程序中使用Mongoose Middleware进行搜索。
我想用Moongose Middleware keyword option搜索一个单词:tags.text,description和spot.name
filters = {
filters : {
mandatory : {
exact : {
municipality: mongoose.Types.ObjectId(req.user.municipality.id)
}
},
keyword : {
fields: ['tags.text','description','spot.name'],
term:word
}
}
};
对于字段tags.text和description工作正常。
但是字段“spot”是一个ObjectID,而不是一个文档字段,而且我正在填充,它正在工作,但是在关键字选项中的字段spot.name不起作用...
有什么建议吗?
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);
}
});
也许不是一个优雅的解决方案,但是我目前唯一找到的解决方案。
首先,我用这个词搜索了Spot,然后使用这样的过滤器搜索了Training:
exact:{spot: spotThatIFoundId}