所以我有两个集合,一个是我最喜欢的,另一个是用户表,其中添加了人才。并且查询搜索不起作用。谁能帮我解决这个问题吗
const list = await FavouriteModel.aggregate([
{
$match: cond,
},
{
$project:{
userId: 1,
talentId: 1,
}
},
{
$lookup: {
from: "users",
localField: "talentId",
foreignField: "_id",
as: "talentId",
pipeline: [
{
$match: {
$expr: {
$eq: ["$fullName", new RegExp('^' + search, 'i')]
},
},
},
{
$lookup: {
from: "venues",
localField: "homeVenue",
foreignField: "_id",
as: "homeVenue",
pipeline: [
{
$project: {
venueName: 1,
},
},
],
},
},
{
$unwind: "$homeVenue",
},
{
$lookup: {
from: "masters",
localField: "profession",
foreignField: "_id",
as: "profession",
pipeline: [
{
$project: {
labelName: 1,
},
},
],
},
},
{
$unwind: "$profession",
},
{
$project: {
fullName: 1,
profileImg: 1,
profession: 1,
homeVenue: 1,
},
},
],
},
},
{
$unwind: "$talentId",
},
]).catch((err) => {
reject(err);
});
在查找管道内,我添加了正则表达式以与全名匹配。一旦用户搜索,它就会开始搜索。请在这方面需要帮助。先谢谢了
首先尝试创建管道并根据条件将代码推送到管道数组中,
你可以使用这个方法
if(search){
pipeline.push({
$match:{
"talendId.fullname": {$regex: search, $options: 'i'},
}
})
}
这不是为了跑这里,只是为了让您理解。