0

I'm trying to retrieve all of my charts data with a query on date attribute but it returns all of charts data and not good results.

Model

charts: [
        {
            date: {
                type: Date,
                required: true,
            },
        },
    ],

My query

Model.findById({ _id: '5f47f7d65ff89941ecbc8340', 'charts.date': { $gte: '2020-01-15' } })

I want to retrieve all charts elements whose date is greater than '2020-01-15'.

Benjamin Le Gallo
  • 87
  • 1
  • 3
  • 10

1 Answers1

0

The find function and its related friends operate on documents. The query you've submitted will match on any document with the matching _id and a charts.date in the specified range, and return the entire document.

If you can get by with just the first matching element from the chart array, you could use $elemMatch in a projection.

If you need to get all matching elements, you'll need to use aggregation with a $filter.

Joe
  • 19,071
  • 2
  • 15
  • 38