所以我想做的是用猫鼬对mongoDb进行“ AND”搜索,但是结果来自“ OR”搜索。有这个设置吗?这是查询的样子:

exampleModel.find({
    $text: {
        $search: searchParams
    }
}, {
    score: {
        $meta: "textScore"
    }
}, { lean: true }).select(exampleViewModel).limit(1000).sort({
    score: {
        $meta: 'textScore'
    }
}).exec(function (err, results) {
   next(null, results);
});


假设searchParams =伦敦餐厅;

这将在带有“ restaurant”或“ london”字样的文档中产生结果。我希望它在包含“ restaurant”和“ london”字样的文档中产生结果。

最佳答案

在搜索词前后加上引号,以将默认行为更改为AND。

https://docs.mongodb.org/manual/reference/operator/query/text/#phrases

exampleModel.find({
    $text: {
        $search: "\"restaurant\" \"london\""
    }
}, {
    score: {
        $meta: "textScore"
    }
}, { lean: true }).select(exampleViewModel).limit(1000).sort({
    score: {
        $meta: 'textScore'
    }
}).exec(function (err, results) {
   next(null, results);
});

09-18 19:12