I have a situation that i need to filter users based on their Profession, here is how the User Schema is defined:
const userSchema = new mongoose.Schema(
{
userData: {
professions: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Profession'
}
]
}
}
)
and here we have for Professions schema:
const ProfessionSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
}
}
)
How can I get a list of users that have a profession with an id of 5f8ea4396586f1168ab3a298
for example?
I tried
User.find({
"userData.professions": { $elemMatch: $in: ["5f8ea4396586f1168ab3a298"] }
})
But seems like this is the opposite of what I'm trying to do.
I need to get a result like this when I filter results based on 5f8ea4396586f1168ab3a298
as id of profession
[
{
"name": "John Doe",
"userData": {
"professions": ["5f8ea4396586f1168ab3a298", "5f8ea4226586f1168ab3a296"]
}
},
{
"name": "John Smith",
"userData": {
"professions": ["5f8ea4396586f1168ab3a298",]
}
},
{
"name": "Elon Musk",
"userData": {
"professions": ["5f8ea4466586f1168ab3a29a", "5f8ea4396586f1168ab3a298", "5f8ea4146586f1168ab3a295"]
}
}
]
You just need to convert your professions id type string to object type using mongoose.Types.ObjectId
,
second you missed {}
braces after $elemMatch
,
let professions = ["5f8ea4396586f1168ab3a298"];
professions = professions.map(profession => mongoose.Types.ObjectId(profession));
User.find({
"userData.professions": { $elemMatch: { $in: professions } }
})
Second option, If you are matching single profession then this is alternative option,
let professions = "5f8ea4396586f1168ab3a298";
User.find({
"userData.professions": professions }
})