skip to Main Content

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-cartsPhoenixMindSubscriptionServicenode_modulesmongodbnode_modulesbsonlibbson.cjs:3670:23)
rvicenode_modulesmongodblibcmapcommands.js:377:37)
at Msg.toBin (D:beyond-cartsPhoenixMindSubscriptionServicenode_modulesmongodblibcmapcommands.js:366:29)
at MessageStream.writeCommand (D:beyond-cartsPhoenixMindSubscriptionServicenode_modulesmongodblibcmapmessage_stream.js:43:34)
at write (D:beyond-cartsPhoenixMindSubscriptionServicenode_modulesmongodblibcmapconnection.js:479:30)
at Connection.command (D:beyond-cartsPhoenixMindSubscriptionServicenode_modulesmongodblibcmapconnection.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?

2

Answers


  1. I had the same problem in one off my Node.js application after upgrading mongodb/mongoose packages.

    The solution to my specific error was rather simple allthough the error message did nom 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!

    Login or Signup to reply.
  2. I recently encountered the same issue while using connect-mongodb-session in my project, receiving an error message stating, "Unsupported BSON version, bson types must be from bson 5.x.x at E:Web DevelopmentClassesPracticeauthChecknode_modulesconnect-mongodb-sessionindex.js:224:19 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)".

    Although my previous project, which utilized the same dependencies, was functioning correctly, I identified the problem by comparing the configurations of the two projects. In the previous project, I had connect-mongodb-session version ^3.1.1, whereas the current project, exhibiting the error, was using version ^4…

    To resolve the issue, I opted to downgrade the connect-mongodb-session version to 3.1.1. Following this adjustment, the project operated without errors. It seems that the latest version of connect-mongodb-session may not be compatible when handling BSON types.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search