Filter on subdocument property
Opened this issue · 2 comments
kaboume commented
Hi,
// Stage Schema
const stageSchema = mongoose.Schema(
{
...
federation: federationSchema,
...
// Subdocument Schema
const federationSchema = mongoose.Schema({
nom: {
type: String,
required: true,
trim: true,
},
groupeId: {
type: mongoose.ObjectId,
ref: 'Groupe',
required: false,
},
type: {
type: String,
required: false,
},
});
I want to select all the stages that have a name of federation equal to "toto"
So I tried :
const filter = {}
filter.federation = { stage: 'toto'}
..
await Stage.find(filter).
But 0 stages are returned...
Is my filter correct ?
gustavomorinaga commented
Same issue
aravindnc commented
The issue with your filter is that it doesn't correctly reference the nested field inside the federation subdocument. When querying for a specific field within a nested subdocument, you need to use dot notation to access the nested field.
const filter = { 'federation.nom': 'test' };
const stages = await Stage.find(filter);