-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.x behavior change due to change in bson
] Mongoose Types.ObjectId.isValid is now more lax, and now returns true for objects that are "new ObjectId"-able
#11419
Comments
Whoopsie, should have commented in dup issue #11301 . Sorry, forgot to search before posting. Well, I think my bug:
As for a workaround, do you have anything to suggest to differentiate between a full object and an "ID-ish" (objectId or string with right shape)? For now, I just inlined bson 1.1.6's And that bug reminded of Hyrum's Law:
|
@Maximusya thanks. Yeah, did the same as you:
@ Mongoose maintainers: leaving this issue open, as it documents the reason to want the old behavior but not the new one (wanting to differentiate between "just an ID-ish ObjectID/string" vs. a "full document with data"), and it pushes forwards the extra requests to:
|
We're adding |
@vkarpov15 perfect. As soon as 6.2.5 is released, will de-inline my |
@vkarpov15 one more thing.
are all great. But shouldn't the below be true? It was with the old 5.x
|
@ronjouch that depends on how you've installed |
@vkarpov15 sure.
|
@ronjouch I set up an attempt at similar project, with an npm module that lists Mongoose as a dependency.
Maybe try putting Mongoose in |
Thanks for trying, Valeri. I monkeyed around again a bit this morning, without success. Closing for now, will re-open if I have something more reproducible. |
Feature request or bug?
Bug / behavior change in mongoose 6.x, which brings
mongodb
4.x, which bringsbson
4.xBehavior change
Mongoose exposes Mongo ObjectId as
Types.ObjectId
and itsisValid
method itself coming frombson
, and its behavior changed in the latest release.In OLD Mongoose 5.x / mongodb 3.x / bson 1.x,
And that makes sense, in the sense that this
o
object is not a valid ObjectId. It's an object that can definitely, with enough effort, be interpreted as an ObjectId, but it's not an ObjectId. Let's look at the bson v1.1.6 code:https://github.com/mongodb/js-bson/blob/v1.1.6/lib/bson/objectid.js#L334-L368
Now, in NEW Mongoose 6.x / mongodb 4.x / bson 4.x,
And that kinda makes sense too, if we interpret
isValid
as "can be interpreted as an ObjectId with enough effort. But that's a behavior change. Let's look at the less strict bson v4.6.1 code:https://github.com/mongodb/js-bson/blob/v4.6.1/src/objectid.ts#L288-L302
Last words
I agree that, strictly speaking, this isn't Mongoose we're talking about here, it's
bson
.That being said, Mongoose does expose directly bson's
isValid
method as mongooseTypes.ObjectId.isValid
, so from my mongoose user's perspective it's a breaking change, one that breaks my code testing if a thing is an ID or a populated object. So, I'd loooove to be informed about the behavior change in the release notes.→ Same request as in #11038 : can you document the behavior change in your Migrating to 6.x guide ?
Thanks for Mongoose!
The text was updated successfully, but these errors were encountered: