Cast to date failed for a $gte filter
See original GitHub issueDo you want to request a feature or report a bug? I want to report a bug
What is the current behavior? I have the following piece of code:
await Connections.find({ connections: { discord: { expires: { $gte: new Date() } } } })
with connections having the following model:
usersdb.model(
"connections",
new mongoose.Schema({
userId: { type: String, unique: true },
connections: {
type: new mongoose.Schema({
discord: {
type: new mongoose.Schema({
expires: { type: Date },
token: { type: String },
refreshToken: { type: String }
})
}
}),
default: () => ({})
}
})
)
Running that, gives me the following error:
CastError: Cast to date failed for value "{ '$gte': 2022-04-10T23:04:34.662Z }" (type Object) at path "expires" for model "connections"
(Full error available here)
What is the expected behavior? The code would run fine with no errors at all and return the documents matching the filter query.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node.JS - 16.10.0 Mongoose - 6.2.10 MongoDB - 5.0.6
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
Node.js and Mongoose - Cast to date failed for value (filtering ...
Short answer: It should be $lte , not lte . Long answer: There is pretty good tutorial what you need to know about...
Read more >Node.js and Mongoose - Cast to date failed ... - appsloveworld
Im creating a search endpoint that will filter the results via a date range. http://0.0.0.0:3000/api/v1/transactions/search?endDate=2018-10-03T14:07:03.382Z.
Read more >Getting date cast error on using $lte & $gte - Glitch Help
Hello! So whenever I use $lte & $gte I get casting error for dates via mongoose. This seems something specific to glitch.
Read more >CastError: Cast to Number failed for value "undefined" (type ...
i have succeded to build the app localy however on replit i got this mongodb error CastError: Cast to Number failed for value...
Read more >Mongoose Tutorials: Working With Dates
When you create a user document, Mongoose will cast the value to a native JavaScript date using the Date() constructor. const user =...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Incorrect query syntax. You should query by
expires.moment
instead as shown below.The MongoDB server interprets
Test.find({expires:{moment:{$gte: date}}})
as “find all documents whereexpires
is exactly equal to the object{ moment: { $gte: Date('2022-04-11T20:17:45.481Z') } }
”. In general, you should use dotted path names instead of nested objects for queries.This worked for me, thanks 😃