Let me first of all explain the situation. I have made advanced search which is used to search posts and profiles. I have made switch button which shows post search or profile search. After receiving all the details on the server. I have made this query:
await Profile.aggregate([
{
$match: {
$or: [
{"company": {$regex: profile.company}},
{"skills": {$all: profile.skills}},
{"experience.company" :{$in: [ profile.experience.company]}}
]
}
},
{
$skip: req.params.page ? req.params.page - 1 : 0 // make sure this can't be -1
},
{
$limit: 11
},
{
$lookup: {
from: "user",
localField: "user",
foreignField: "_id",
as: "user"
}
},
{
"$unwind": "$user"
}
])
But lets say company is undefined and gives me the following error $regex must be a string. How to search about this fields that are defined or not empty string? Also I'm not sure if that piece of code is right:
{"experience.company" :{$in: [ profile.experience.company]}}
And the next question is how search for user.name company and skills at once? Should I lookup first and then match or what? (Sorry about this questions, but I'm new to MongoDB)