Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter on subdocument property #192

Open
kaboume opened this issue Mar 20, 2023 · 2 comments
Open

Filter on subdocument property #192

kaboume opened this issue Mar 20, 2023 · 2 comments

Comments

@kaboume
Copy link

kaboume commented Mar 20, 2023

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
Copy link

Same issue

@aravindnc
Copy link
Owner

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants