Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5756

ObjectId instances created by bson and mongodb are not interchangeable

    • 0
    • Not Needed
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      If I import ObjectId from the bson package and compare those instances against the ones created by ObjectId from the mongodb package, an exception is thrown, as follows:

      node_modules/bson/lib/bson.mjs:2118
                  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
                                                       ^TypeError: Cannot read properties of undefined (reading '11')
          at ObjectId.equals (file:node_modules/bson/lib/bson.mjs:2118:50)
          at file:///dist/ttt.js:9:4 

      The set up is an internal package that implements all interactions with Mongo DB and the application package that passes ObjectId instances into methods of the internal package.  The intent here is that the application does not need or should access the database directly and only uses specific types, like ObjectId.

      This is npm output for both, bson and mongodb.

      npm list bson
      app@0.19.0
      +-- bson@6.2.0
      `-- internal-package@0.8.0
        `-- mongodb@6.3.0
          `-- bson@6.2.0 deduped
      npm list mongodb
      app@0.19.0
      `-- internal-package@0.8.0
        `-- mongodb@6.3.0 

      Here's the code to reproduce an error. It's TypeScript that is built with node16 modules, for NodeJS v16.20.2.

      import { ObjectId } from "mongodb";
      import { ObjectId as BsonObjId} from "bson";
      
      let o1: BsonObjId = BsonObjId.createFromHexString("61c4a0a1033ad3de068c5854");
      let o2: ObjectId = ObjectId.createFromHexString("61c4a0a1033ad3de068c5854");
      let o3: BsonObjId = BsonObjId.createFromHexString("61c4a0a1033ad3de068c5854");
      let o4: ObjectId = ObjectId.createFromHexString("61c4a0a1033ad3de068c5854");
      
      // works
      o1.equals(o3);
      o2.equals(o4);
      
      // throws
      o1.equals(o2);
      o2.equals(o3); 

      The problem appears to be in otherId[kId] always being undefined, perhaps because kId is mixed in somehow between modules.

      This used to work for years and got broken for me when I updated Mongo DB to the version listed above.

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            cis74633@bell.net Andre M
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: