0

Schema:

new Schema({
    productId: String,
    types: [{
        productType: String,
        lastModified: Date
    }]
});

Query:

{
  productId: "1",
  email: "[email protected]",
  productType: "test",
}

I tried this but its returning only first matched element:

const productType = 'test';
const result = await this.model(email)
.find(
    { productId, 'types.productType': productType },
    { 'types.$': productType }
).lean();

with aggregate, it return empty array result:

const result = await this.model(email).aggregate([
    { $match: { productId, 'types.productType': 'productType' } },
    {
        $project: {
            types: {
                $filter: {
                    input: '$types',
                    as: 'r',
                    cond: { $eq: ['$$r.productType', productType] }
                }
            },
            _id: 0
        }
    }
]);

I need to find all matching elements where projection $ returns the first matched

kittu
  • 6,662
  • 21
  • 91
  • 185

0 Answers0