Mongodb-如何在查找查询中使用聚合文本搜索

Mongodb-如何在查找查询中使用聚合文本搜索,mongodb,mongoose,mongodb-aggregation,Mongodb,Mongoose,Mongodb Aggregation,我在Mongodb中对文本搜索使用聚合方法。我尝试了各种方法,但仍然找不到正确的方法来过滤我的结果。我已经设置了一个索引,它只适用于$text搜索,也适用于查询 以下是我执行文本搜索的代码: Model.aggregate([ { $match: { $text: { $search: searchValue } } }, { $project: { displayTitle: 1, title: 1, body: 1, _type: 1, _id: 1, score: { $m

我在Mongodb中对文本搜索使用聚合方法。我尝试了各种方法,但仍然找不到正确的方法来过滤我的结果。我已经设置了一个索引,它只适用于$text搜索,也适用于查询

以下是我执行文本搜索的代码:

Model.aggregate([
    { $match: { $text: { $search: searchValue } } },
    { $project: { displayTitle: 1, title: 1, body: 1, _type: 1, _id: 1, score: { $meta: "textScore" } } },
    { $match: { score: { $gt: 1.0 } } }
], function (err, models) {

})
但是,我希望能够通过此查询进一步筛选模型:

Model.find({_parentIds: {$in: arrayOfIds}})
我本以为这会奏效:

Model.aggregate([
    { $match: { $text: { $search: searchValue }, _parentIds: {$in: arrayOfIds} } },
    { $project: { displayTitle: 1, title: 1, body: 1, _type: 1, _id: 1, score: { $meta: "textScore" } } },
    { $match: { score: { $gt: 1.0 } } }
])
但遗憾的是,事实并非如此。有没有人试过这个,或者我错过了什么

以下是我正在搜索的示例集合:

[{
    displayTitle: "first item",
    title: "first_item",
    _type: "item",
    _parentIds: ["123", "234"]
}, {
    displayTitle: "second item",
    title: "second_item",
    _type: "item",
    _parentIds: ["123", "234"]
}, {
    displayTitle: "third item",
    title: "third_item",
    _type: "item",
    _parentIds: ["345", "456"]
}]
我当前的搜索结果如下:

searchValue = "item"
arrayOfIds = ["345"];
并且希望这份文件能回来:

{
    displayTitle: "third item",
    title: "third_item",
    _type: "item",
    _parentIds: ["345", "456"]
}

谢谢

文本分数为0.75。因此,将匹配过滤器更改为大于0.5是可行的

修改投影以排除主体、\u id和包含父id

已使用此查询创建索引

db.textcol.createIndex( { displayTitle: "text" } )
db.textcol.aggregate([
    { $match: { $text: { $search: "item" }, _parentIds: {$in: ["345"]} }} ,
    { $project: { displayTitle: 1, title: 1, _type: 1, _id: 0, _parentIds :1, score: { $meta: "textScore" } }},
        { $match: { score: { $gt: 0.5 } } }
])
正在运行此查询

db.textcol.createIndex( { displayTitle: "text" } )
db.textcol.aggregate([
    { $match: { $text: { $search: "item" }, _parentIds: {$in: ["345"]} }} ,
    { $project: { displayTitle: 1, title: 1, _type: 1, _id: 0, _parentIds :1, score: { $meta: "textScore" } }},
        { $match: { score: { $gt: 0.5 } } }
])
输出:

{
    "displayTitle": "third item",
    "title": "third_item",
    "_type": "item",
    "_parentIds": ["345", "456"],
    "score": 0.75
}

你能添加样本文档和预期输出吗?@Veeram-我已经添加了样本集合和预期输出谢谢!!!这救了我!所以,我的实际问题应该更清楚一点,那就是它返回了我不想返回的结果(所以在_parentId数组中没有_parentId的模型)。上面的解决方案修复了它,主要原因是$projects中的_parentId:1和_id:0,并且分数的$match太高了-不确定我怎么看不到。。。