Reputation: 93
I have a mongo schema as follows.
var employeeSchema = new mongoose.Schema({
id: { type: String,ref:'Users' },
FirstName: { type: String },
LastName: { type: String },
Skills: [
{
Category: { type: String },
Level: { type: String }
}
]}, { strict: true });
After Entering data it looks like this.
{
id: '123456',
FirstName: "Name",
LastName: "Last Name",
Skills: [
{
Category: "Traffic Management Supervisor ",
Level: "Intermediate"
}
]
}
My problem i want to filter employers by his skill category. And i want to filter employees by first letter of each word instead of entering whole string
Ex: I need to filter all employees by their skill category using TMS instead of Traffic Management Supervisor
Upvotes: 0
Views: 1779
Reputation: 1428
You can use regex match in the aggregate method. I would suggest that should you create those searches dynamically. Whatever input is and push them into $or and regex...
// Only filter by category field
Employee.aggregate([{
$match: {
"Skills.Category": {
$regex: '[tms]+',
$options: 'i'
}
}
}])
// Any category match
Employee.aggregate([{
$match: {
$or: [{
"Skills.Category": {
$regex: '[tms]+',
$options: 'i'
}
}, {
"Skills.Level": {
$regex: '[tms]+',
$options: 'i'
}
}]
}
}])
Upvotes: 2