How to add query 'greater than' date in match aggregation query in mongoose

Viewed 697

I need add date from to get response from mongo match.

my schema in db:

schedule: {
  creationDate: "2020-12-06T00:48:38.000Z",
  creationUser: "5fcc23936ce8678e43d35b06",
  end: "2020-12-06T00:00:00.000Z",
  operationType: "C",
  start: "2020-12-05T20:00:00.000Z",
  _id: "5fcc2a664d18280025ac0acb"
}

my query:

Object.keys(params.filter).map((key) => {
  if(key === 'start') {
    match[key] = {
      $gte: new Date(params.filter[key])
    }
  }
})

my result:

[]
1 Answers

I solved the query. what I needed to do was create an aggregate for a match in mongodb, which can bring dates greater than the one entered by the user. Only CLI samples appear in the mongo documentation. So I came here for help. But I found the answer by creating the query in the constructor that mongo atlas provides. Here I share it. I love you so much.

"creationDate": {
    "$gte": {
      "$date": "2020-12-01T03:00:00Z"
    },
    "$lte": {
      "$date": "2020-12-31T03:00:00Z"
    }
  }

Translated to my code specifically, the solution is this way:

Object.keys(params.filter).map((key) => {
  if(key === 'employee') {
    match[key] = {
      $oid: params.filter[key]
    }
  }
  if(key === 'start') {
    match[key] = {
      $gte: {
        $date: params.filter[key]
      }
    }
  }
})
Related