I have tried to filter out nested array elements inside a document, but nothing is showing.
Here is my schema:
//Product:
const productSchema = new mongoose.Schema(
{
productname: {
type: String,
required: [true, 'User must have a name'],
unique: true,
validate: {
validator: function (str) {
return validator.isAlphanumeric(str, 'en-US', { ignore: ' ' });
},
message: (props) => `${props.value} is not a valid username`,
},
},
slug: String,
price: {
type: Number,
required: [true, 'A product must have a price'],
},
description: {
type: String,
trim: true,
},
images: [String],
variants: [Variant], //Schema
},
{
id: false,
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
//Variant:
const variantSchema = new mongoose.Schema(
{
// product: {
// type: mongoose.Schema.ObjectId,
// ref: 'Product',
// },
// size: {
// type: mongoose.Schema.ObjectId,
// ref: 'Size',
// },
// color: {
// type: mongoose.Schema.ObjectId,
// ref: 'Color',
// },
size: {
type: String,
enum: {
values: [
'35',
'35.5',
'36',
'36.5',
'37',
'37.5',
'38',
'38.5',
'39',
'39.5',
'40',
'41',
'41.5',
'42',
'42.5',
'43',
'44',
'44.5',
'45',
'46',
'46.5',
'47',
'47.5',
'48',
'S',
'M',
'L',
'XL',
'XXL',
'XS',
'Onesize',
],
message: 'Please enter correct sizing format !',
},
required: [true, 'Please enter sizing !'],
},
color: { type: String, required: [true, 'Please enter color !'] },
quantity: Number,
},
{
id: false,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
Note: variants is embedded so is exported as Schema.
So I'm trying to filter out variant with color Grey like in the pic: 
What I did was finding the product by slug and filtering using the color property inside variant.
const document = await Model.find({
slug: req.params.slug,
variants: {
color: 'Grey'
}
});
It shows me nothing, 0
I also tried with "variants.color" : "Grey"
But this time it gives me all the result. So it seems filter didnt apply or simply not worked.