我在Mongodb中使用聚合方法對文本搜索。我已經嘗試過各種方式,仍然無法找到正確的方法來過濾我的結果。我已經設置了一個索引,並且只需$ text搜索就可以正常工作,並且只需查詢即可正常工作。Mongodb - 如何使用聚合文本搜索與查詢查詢
這裏是我的代碼做一個文本搜索:
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"]
}
謝謝!
能否請你添加樣本文檔和預期產出? – Veeram
@Veeram - 我添加了一個樣本集合和預期的輸出 – darylhedley