【问题标题】:Mongoose Middleware: Keyword with populationMongoose 中间件:带有人口的关键字
【发布时间】:2018-01-04 07:14:50
【问题描述】:

我在 MEAN.JS 应用程序中使用 Mongoose 中间件进行搜索。

我想用Moongose Middleware keyword option 搜索以下字段:tags.textdescriptionspot.name

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

对于字段 tags.textdescription 有效。

但是字段“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);


        }
    });

【问题讨论】:

  • 我现在也有同样的任务,请问您有什么更好的解决方案吗?
  • 对不起,我不记得了。这个问题是 3 年前的问题。

标签: node.js mongodb express mongoose meanjs


【解决方案1】:

也许不是一个优雅的解决方案,但我目前发现的唯一一个。

首先我用这个词搜索了 Spot,然后,使用如下过滤器搜索了 Training

exact:{spot: spotThatIFoundId}

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 2014-02-19
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2018-12-13
    • 1970-01-01
    • 2012-02-27
    相关资源
    最近更新 更多