在聚合中,如果两个字段包含(一个或另一个)相同的字符串变量,我想搜索两个字段。但是搜索仅在一个字段上完成。
这是我的代码:
const textFilter = req.body.freeTextFilter !== '' ? `.*${req.body.freeTextFilter}*.` : '.';
...
{
$match: {
$or: [
{ name: { $regex: textFilter, $options: 'i' } },
{ comment: { $regex: textFilter, $options: 'i' } }
]
}
},
...
我在做什么错?搜索仅在评论字段上完成。
模式如下所示(还有更多字段):
const Activity = new Schema({
name: {
type: String,
required: true
},
comment: {
type: String
},
},
如果一个文档的字段“名称”包含特定的文本,而另一个文档的字段“注释”包含相同的特定文本,则我希望查询返回两个文档。 如果我仅在“名称”字段中搜索(没有$ or),则不会返回任何内容。
答案 0 :(得分:0)
好,问题解决了。查询工作正常,问题出在其他地方。正如我之前写的,我没有得到正确的结果,但这是由于分页逻辑错误。