[英]How could i concatenate two strings in my search box in node js?
Here is my code where my search box searches people only if i enter firstname or the lastname, but i also want it to display results if i enter "firstname lastname" together.这是我的代码,只有当我输入名字或姓氏时,我的搜索框才会搜索人,但如果我一起输入“名字姓氏”,我也希望它显示结果。 Can somebody suggest what parameter should i put in collection.find({}) ?
有人可以建议我应该在 collection.find({}) 中放入什么参数吗? PLEASE HELP !
请帮忙 !
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
async website(req, res) {
if(req.query.search){
const regEx = new RegExp(escapeRegExp(req.query.search), "gi");
await Web.find( {$or: [{firstname: regEx}, {lastname: regEx}] }, function(err, web) {
if(err)
{
console.log(err);
res.send("error", "Oops, Something went wrong! Please try again.");
}
else
{
res.render("./web/webProfiles", { web });
}
});
}
}
await Web.aggregate([
{
"$group":{
_id:"$_id",
"full_name":{$first:{$concat:['$firstname',' ',"$lastname"]}},
"firstname"{$first:"$firstname"},
"lastname"{$first:"$firstname"},
}
},
{$match:{$or: [{firstname: regEx}, {lastname: regEx},{full_name:regEx}] }}
]);
you can check mongodb aggregation and concat. In Group function you can write what you want in your response.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.