question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cast to date failed for a $gte filter

See original GitHub issue

Do 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:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, May 6, 2022

Incorrect query syntax. You should query by expires.moment instead as shown below.

console.log(await Test.find({'expires.moment':{$gte: date}})

The MongoDB server interprets Test.find({expires:{moment:{$gte: date}}}) as “find all documents where expires 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.

0reactions
arthursb2016commented, May 23, 2022

Incorrect query syntax. You should query by expires.moment instead as shown below.

console.log(await Test.find({'expires.moment':{$gte: date}})

The MongoDB server interprets Test.find({expires:{moment:{$gte: date}}}) as “find all documents where expires 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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found