unknown163
unknown163

Reputation: 57

Mongoose - why cast date is failed?

I want to filter values from the collections with range of 2 dates:

const filterPattern = {
  createdAt: {
    '&gte': 2020-10-09T14:18:07.240Z,
    '&lte': 2020-12-09T14:26:49.544Z
  }
};

CollectionName.find(filterPattern);

But get an error:

CastError: Cast to date failed for value "{ '&gte': 2020-10-09T, '&lte': 2020-12-09T14:26:49.544Z }" at path "createdAt" for...

Both of the values '&gte' and '&lte' I've created by new Date() and they return true after instanceof Date.

What wrong there?

Upvotes: 1

Views: 615

Answers (1)

eol
eol

Reputation: 24565

You have to use $ instead of & and set the values accordingly:

const filterPattern = {
  createdAt: {
     $gte: "2020-10-09T14:18:07.240Z",
     $lte: "2020-12-09T14:26:49.544Z"
  }
};

Upvotes: 1

Related Questions