Description:
I'm encountering a BSONVersionError in my Node.js application, and the error message states "Unsupported BSON version, bson types must be from bson 5.x.x." I believe this is related to the MongoDB Node.js driver or another library using BSON.
Details:
npm install [email protected].Code Snippets: dbClientObj.collection(CollectionName).findOne({_id:_id: new ObjectId(employeeId),
Error Stack Trace:
BSONVersionError: Unsupported BSON version, bson types must be from bson 5.x.x
at serializeInto (D:\beyond-carts\PhoenixMind\SubscriptionService\node_modules\mongodb\node_modules\bson\lib\bson.cjs:3670:23)
rvice\node_modules\mongodb\lib\cmap\commands.js:377:37)
at Msg.toBin (D:\beyond-carts\PhoenixMind\SubscriptionService\node_modules\mongodb\lib\cmap\commands.js:366:29)
at MessageStream.writeCommand (D:\beyond-carts\PhoenixMind\SubscriptionService\node_modules\mongodb\lib\cmap\message_stream.js:43:34)
at write (D:\beyond-carts\PhoenixMind\SubscriptionService\node_modules\mongodb\lib\cmap\connection.js:479:30)
at Connection.command (D:\beyond-carts\PhoenixMind\SubscriptionService\node_modules\mongodb\lib\cmap\connection.js:312:13)
Environment:
What I've Tried: I have tired using bson package like new BSON.ObjectId(<employee_id>)
Question:
How can I resolve the BSONVersionError and make sure my application is using BSON types from version 5.x.x?
I had the same problem in one of my Node.js applications after upgrading mongodb/mongoose packages.
The solution to my specific error was rather simple although the error message did not show any hint as also discovered by you.
After looking into the code I quickly realized it was due to use of
new ObjectId(...)
in my aggregation pipelines.
I imported ObjectId from mongodb package. That was the problem in new mongoose version: Had to import ObjectId from mongoose instead like so:
import { Types } from 'mongoose'
...
... new Types.ObjectId(...)
Maybe that helps someone if not you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With