Date coercion and typechecking inconsistent on bulkWrite
#14400
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
typescript
Types or Types-test related issue / Pull Request
Milestone
Prerequisites
Mongoose version
8.2.0
Node.js version
18.19.0
MongoDB server version
6.0
Typescript version (if applicable)
5.3.3
Description
Different methods of using
bulkWrite
to update a singleDate
property are inconsistent in their typechecking and coercion to Dates in the DB when passing an ISOString.The closest mention here is #11773, but I also wanted to flag that this issue appears with
.bulkWrite
as well and how it interacts with typechecking, since there is some degree of validation depending on what level of the list one is working with.Steps to Reproduce
I ran the following code to compare various cases. Some cases type check, when trying to use
updateOne
withinbulkWrite
, others don't. In addition, there is at least one case of usingupdateOne
withinbulkWrite
that saves the Date as a string as opposed to aDate
.Expected Behavior
All of these cases would be consistent in their typechecking and Date casting.
I had a bit of trouble figuring out when the typechecking was being applied based on the docs alone, and the interaction between aggregation pipelines, $set, bulkWrite, casting, and lean() made juggling the expected behavior based on the docs a bit tricky.
These docs reference that dates are casted in some cases. It might be helpful to explain when casting happens on writes and updates.
It is great that these docs explicitly say that casting isn't performed in aggregation stages. It may not be obvious to some users that using a list for
updateOne
withinbulkWrite
means that an aggregation pipeline is being used and thus validators are being turned off. Don't know if there's a great solution here unfortunately.Using lean does mention that there is a lack of casting, which is nice.
I was also surprised that none of these cases throw an error:
https://mongoosejs.com/docs/validation.html mentions that validators are run on
$set
. I guess it is because of the casting.The text was updated successfully, but these errors were encountered: