Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BSONVersionError: Unsupported BSON version, bson types must be from bson 5.x.x in Node.js application

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:

  • I've checked my dependencies, and I'm using MongoDB Node.js driver version [your version].
  • I've already attempted to update the MongoDB Node.js driver to version 5.x.x using npm install [email protected].
  • I'm running my application against MongoDB server version 5.9.2

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:

  • Node.js version: 18
  • MongoDB server version: 5.9.2

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?

like image 301
karthik Rao Avatar asked Oct 31 '25 14:10

karthik Rao


1 Answers

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!

like image 140
Torsten Barthel Avatar answered Nov 03 '25 03:11

Torsten Barthel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!