[QUESTION]: isEqual and mongodb ObjectId #5895
-
Hi, why would the following test sample not log
I asked the question to MongoDB directly and this is the response I have received based on my test sample.
The first block that uses the native equals method prints true for each comparison, while using lodash-es's isEqual method returns false when using id2 in the comparison. If anyone can help it would be appreciated. I just find it off that when stringifying and parsing and doing the comparison using the same ObjectId from bson library works but not when using the one from mongodb library which internally exports the bson one. UPDATE I sort of found a way to fix it (patch it) in my case but I don't really like the approach. Here is the test cases to show what I mean.
In this exmaple, you will see, I am only stringifying and parsing 2 ObjectIds created with the mongodb and bson export respectively. When I try to compare the objId (made with mongodb) with the EJSON.parse, it returns false because it wasn't made with the same instance. But at the end, when I override it with the same method from the mongodb export, it works, even if the last test returns false. In my case, my API inputs ObjectIds are transformed using the ObjectId instance from mongodb (not bson). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I just got an update from MongoDB and here is what they said: Such discrepancy in behaviour starting from 5.0.0 is due to a change in how BSON is bundled starting from bson@5.0.0. We now provide two BSON bundles: One in CommonJS. For example, importing BSON from "mongodb" instead of from "bson" would result in both ObjectIds being the same:
I will leave it here in case anyone else encounters this issue. |
Beta Was this translation helpful? Give feedback.
I just got an update from MongoDB and here is what they said:
Such discrepancy in behaviour starting from 5.0.0 is due to a change in how BSON is bundled starting from bson@5.0.0. We now provide two BSON bundles:
One in CommonJS.
One as ESM (ECMAScript Module).
The mongodb package uses and re-exports the CommonJS BSON package. However, since your reproduction is written in ESM syntax, it is importing the ESM BSON bundle. This results in two copies of the BSON library being pulled into memory simultaneously. This discrepancy can be remedied by ensuring that only one instance of the BSON library is used.
For example, importing BSON from "mongodb" instead of from "bson" would result in both …