I am trying to find the list of users based on the different queries. Like if I pass firstname
or lastname
or location
I want data based on one of them or all of them but I've failed to get the details. And also there are 2 nested fields I want to pass for the check. These are here "otherDetails.nationality": "Indian"
and "professionalDetails.type": "Cook"
. Whenever I pass them a single query check I get the desired data but Whenever I put all of them in the $or
aggregation method it fails and gives me all the data in database. I was stuck here for 2 days and couldn't solve it. A help will be appreciated
here is a glimpse of the database data. Don't go with the same its one record I copied multiple times and I also removed some of the fields I make it clean
[{
"_id": "604670a613a06f0017702d88",
"firstname": "Babulal ",
"lastname": "Gadri",
"mobile": "00000000000",
"otherDetails": {
"nationality": "Indian"
},
"professionalDetails": {
"type": "Cook"
}
}
{
"_id": "604670a613a06f0017702d8a",
"firstname": "Babulal ",
"lastname": "Gadri",
"mobile": "00000000000",
"otherDetails": {
"nationality": "Indian"
},
"professionalDetails": {
"type": "Cook"
}
}
{
"_id": "604670a613a06f0017702d8b",
"firstname": "Babulal ",
"lastname": "Gadri",
"mobile": "00000000000",
"otherDetails": {
"nationality": "Indian"
},
"professionalDetails": {
"type": "Cook"
}
}
{
"_id": "604670a613a06f0017702d88",
"firstname": "Babulal ",
"lastname": "Gadri",
"mobile": "00000000000",
"otherDetails": {
"nationality": "Indian"
},
"professionalDetails": {
"type": "Cook"
}
}
{
"_id": "604670a613a06f0017702d8c",
"firstname": "Babulal ",
"lastname": "Gadri",
"mobile": "00000000000",
"otherDetails": {
"nationality": "Indian"
},
"professionalDetails": {
"type": "Chef"
}
}
]
Here is the function I am doing
searchCookForNotification: async function (req, res) {
let { type, nationality, firstname, lastname, location } = req.query;
var db = Person.getDatastore().manager;
var cooks = await db
.collection(Person.tableName)
.find({
$or: [
{ firstname },
{ lastname },
{ location },
{ "professionalDetails.type": type },
{ "otherDetails.nationality": nationality },
],
})
.toArray();
console.log("Length: ", cooks.length);
return res.successResponse(
cooks,
200,
null,
true,
"Cooks found successfully"
);
},
};