首页 > 解决方案 > 如何使用带有正则表达式搜索选项的填充

问题描述

我正在尝试fullName使用 mongoose 在填充字段上实现搜索,这里我使用如下查询,

let search = this.req.body.search ? this.req.body.search : '';
let response = await Miles.find(filter)
    .collation({ locale: Config.collation }).sort(sortBy)
    .populate('driverId', { fullName: 1, longitude: 1, latitude: 1, address: 1, match:  { fullName: { $regex: `.*${search}.*` } } })
    .skip(skip).limit(perPage);

但它抛出"Unknown expression $regex"异常。谁能帮我解决这个问题。

如果有任何与搜索文本匹配的文档,我只需要那个或多个文档。

标签: javascriptnode.jsmongodbmongoosebackend

解决方案


推荐阅读