我使用 MongoDB Atlas Search 搜索資料庫中的資源串列(我使用的是 Mongoose,因此語法略有不同):
const allApprovedSearchResults = await Resource.aggregate([{
$search: {
compound: {
should: [
{
wildcard: {
query: queryStringSegmented,
path: ["title", "link", "creatorName"],
allowAnalyzedField: true,
}
},
{
wildcard: {
query: queryStringSegmented,
path: ["topics"],
allowAnalyzedField: true,
"score": { "boost": { "value": 2 } },
}
}
,
{
wildcard: {
query: queryStringSegmented,
path: ["description"],
allowAnalyzedField: true,
score: { "boost": { "value": .2 } },
}
}
]
}
}
}])
.match(matchFilter)
.exec();
const uniqueLanguagesInSearchResults = [...new Set(allApprovedSearchResults.map(resource => resource.language))];
最后一行檢索結果集中的所有唯一語言。但是,我想要應用之前所有語言的串列.match(matchFilter)。有沒有辦法在沒有過濾器的情況下進行第二次搜索?
uj5u.com熱心網友回復:
您可以在:$facet之后使用 a$search
.aggregate([
{
$search: {
compound: {
should: [
{
wildcard: {
query: queryStringSegmented,
path: ["title", "link", "creatorName"],
allowAnalyzedField: true,
}
},
{
wildcard: {
query: queryStringSegmented,
path: ["topics"],
allowAnalyzedField: true,
"score": { "boost": { "value": 2 } },
}
}
,
{
wildcard: {
query: queryStringSegmented,
path: ["description"],
allowAnalyzedField: true,
score: { "boost": { "value": .2 } },
}
}
]
}
}
},
{
"$facet": {
"filter": [
{$match: matchFilter}
],
"allLanguages ": [
{$group: {_id: 0, all: {$addToSet: '$language'}}}, //<- replace '$language' with real field name
]
}
}
])
您沒有提供結構,所以我假設“語言”是欄位名稱。創建一個分支 - 一個稱為“過濾器”的$facet部分將僅包含過濾后的結果,而另一個稱為 allLanguages 的部分將包含一組所有語言,無論過濾器如何。您可以$project在每個$facet管道中添加步驟來格式化資料.
根據檔案,它應該可以作業:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465390.html
標籤:mongodb 猫鼬 mongodb查询 mongodb-图集 mongodb-atlas-搜索
上一篇:是否可以洗掉陣列中的第一個元素?
