Skip to content

Conversation

@addaleax
Copy link
Contributor

@addaleax addaleax commented Oct 17, 2025

Make Symbol.for('@@mdb.bson.type') available as an alias for _bsontype. This simplifies downstream code by making it a reliable property of deserialized values that cannot conflict with _bsontype in user-provided (sub)documents, which would otherwise need to be detected through an additional prototype comparison.

Description

Summary of Changes

Notes for Reviewers

What is the motivation for this change?

Release Highlight

Release notes highlight

JS classes representing BSON values now have a bsonType symbol property as an alias for the existing _bsontype property. This makes it easier to distinguish between values of a specific BSON type and documents with arbitrary keys after deserializing:

import { bsonType, deserialize } from 'bson';

const doc = deserialize(...);
// will be set when doc.value is a BSON wrapper class **or** a sub-document { _bsontype: '...' } 
console.log(doc.value._bsontype); 
// will be set if and only if doc.value is a BSON wrapper class
console.log(doc.value[bsonType]);

It is safe to replace all uses of value._bsontype with value[bsonType].

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

Make `Symbol.for('@@mdb.bson.type')` available as an alias for
`_bsontype`. This simplifies downstream code by making it a reliable
property of deserialized values that cannot conflict with `_bsontype`
in user-provided (sub)documents, which would otherwise need to be
detected through an additional prototype comparison.
@addaleax addaleax requested a review from a team as a code owner October 17, 2025 14:04
@baileympearson baileympearson merged commit 1e1b619 into mongodb:main Oct 20, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants