Mongoose : filter on an subdocument attribute

24 views Asked by At

I have a subdocument Schema (federation) in my stage Schema :

// 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 = { nom: 'toto'}
..
await Stage.find(filter).

But 0 stages are returned... Is my filter correct ?

1

There are 1 answers

0
Ricou On

Thanks !

filter['federation.nom'] = 'toto';