0

I have a date like :


{
......
title: x
createdAt:"2021-07-02T11:26:08.671+00:00
......
},
{
......
title: x
createdAt:"2021-07-03T11:26:08.671+00:00
......
},
{
......
title: x
createdAt:"2021-07-05T11:26:08.671+00:00
......
},

I want to search data created on 2021-07,year and month matter, how to achieve this? My input date format : 2021-07.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
david
  • 13
  • 1
  • 7
  • Does this answer your question? [Find objects between two dates MongoDB](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) – Matt Aug 02 '21 at 05:28
  • date is ""2021-07-05T11:26:08.671+00:00", and my input is 2021-07 – david Aug 02 '21 at 05:31
  • gte 2021-07-01T00:00:00.000+00:00 and lt 2021-08-01T00:00:00.000+00:00 (utc) – Matt Aug 02 '21 at 05:36

1 Answers1

0

You can run the query using $expr operator:

db.collection.find({$expr: { $and: [
              {
                "$eq": [{"$month": "$createdAt"},7]
              },
              {
                "$eq": [{"$year": "$createdAt"},2021]
              }
          ]})

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35