我正在尝试在 express js 中设置一个无限制的查询参数。但我无法弄清楚我应该如何在我的代码中实现它。我在用着MongoDB aggeration
我想建立用多个$match
阶段搜索的无限方面
像这样工作:
'http://localhost:4000/search?text=mango'
'http://localhost:4000/search?text=mango&key=brand&value=rasna' //unlimited facets.
'http://localhost:4000/search?text=mango&key=brand&value=rasna&key=color&value=yellow' //unlimited facet parameters
这是我的代码:
app.get("/search", async(request, response) => {
try {
const textsearch = request.query.text;
var keystore = request.query.key; //storing `key` in 'keystore'
var valuestore = request.query.value; //storing `value` in `valuestore`
if (keystore, valuestore) {
facetjson = [
{
'$match': {
[keystore]: `${valuestore}` //Storing key and value in $match
}
}
]
const Pipeline = [{
'$search': {
'text': {
'query': `${textsearch}`,
'path': 'title',
}
}
},
{
'$limit': 5
}
]
//Pushing 'facetjson' array into Pipeline array to make a filtered search possible.
const newitem = insert(Pipeline, Pipeline.length - 1, facetjson)
let result = collection.aggregate(newitem).toArray();
response.send(result);
} else {
const Pipeline = [{
'$search': {
'text': {
'query': `${textsearch}`,
'path': 'title',
}
}
},
{
'$limit': 5
}
]
let result = collection.aggregate(Pipeline).toArray();
response.send(result);
};
} catch (error) {
response.status(500).send({ message: error.message });
}
})
(JSFIDDLE 代码示例)[https://jsfiddle.net/divyanshuking/z0vo589e/]
==>我知道我每次都必须为 single $match
, Pair传入数组。做了很多谷歌搜索,我发现我必须使用. 但我不知道如何实现这一点。你们有更好的主意来解决这个问题吗?请帮助我:Pipeline
Key
Value
Rest Parameter
(...keystore,...valuestore)