【发布时间】:2021-02-06 16:11:46
【问题描述】:
我想对 name 执行自动补全,但使用 mongoose 和 nodejs 在特定城市进行过滤。 我有一个这样的 mongodb 集合:
{
"_id" : ObjectId("6007fd9d984e2507ad452cf3"),
"name" : "John",
"city" : "A",
},
{
"_id" : ObjectId("6007ff6844d9e517e1ec0976"),
"name" : "Jack",
"city" : "B",
}
到目前为止我做了什么:
我已经使用搜索索引设置了 MongoDB Atlas(在 search doc 的帮助下) 并像这样设置自动完成:
router.get('/search', async (request, response) => {
try {
let result = await Client.aggregate([
{
"$search": {
"autocomplete": {
"query": `${request.query.term}`,
"path": "name",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 3,
},
},
},
},
{
$limit: 3
},
{
$project: {
"_id": 0,
}
}
]);
response.send(result);
} catch (e) {
response.status(500).send({message: e.message});
}
});
在前端,autocompleteJs:
const autoCompleteJS = new autoComplete({
data: {
src: async () => {
const query = document.querySelector("#autoComplete").value;
const source = await fetch(`${window.location.origin}/search?term=${query}`);
const data = await source.json();
return data;
},
key: ["name"],
},
trigger: {
event: ["input", "focus"],
},
searchEngine: "strict",
highlight: true,
});
到目前为止,它运行良好。但我不知道如何根据城市过滤自动完成结果。 documentation 似乎没有提到这一点。你有什么线索吗?
【问题讨论】:
标签: node.js mongodb mongoose autocomplete mongodb-atlas-search