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

Stream changeEvent reporting wrong data type for documentKey

    • Not Needed

      What problem are you facing?

      I'm watching a stream on my node.js server and need the document _id. However, the typescript lib seem to show the wrong datatype and so I can't get the _id without ugly casts.

      What driver and relevant dependency versions are you using?

      Node.jsĀ 14.17.6
      MongoDB 5.0.2
      (npm) @types/mongodb 4.0.7
      (npm) mongodb 4.1.2

      Steps to reproduce?

      const changeStream = collection.watch(undefined, {  fullDocument: 'updateLookup',})
      changeStream.on('change', (changeEvent: ChangeStreamDocument) => {
        const _id = changeEvent.documentKey._id
      }

      I'm getting the error

      Property '_id' does not exist on type 'ObjectId'.ts(2339)
      

      When hoovering the changeEvent.documentKey it says

      ChangeStreamDocument<Document>.documentKey?: ObjectId | undefined
      

      However, if I log the changeEvent object I get

      {
        "_id": {
          "_data": "82614AC8A6000000022B022C0100296E5A1004FD360DC87FAC4D5F84617FE81B0C267246645F696400646149E99E87EB1BC211F855480004"
        },
        "operationType": "update",
        "clusterTime": {
          "$timestamp": "7010636385245724674"
        },
        "fullDocument": {
          "_id": "6149e99e87eb1bc211f85548",
          ... rest of the document
        },
        "ns": {
          "db": "<database>",
          "coll": "<collection>"
        },
        "documentKey": {
          "_id": "6149e99e87eb1bc211f85548"
        },
        "updateDescription": {
          "updatedFields": {
            ... updated fields
          },
          "removedFields": [],
          "truncatedArrays": []
        }
      }
      

      As you can see, the documentKey is not an ObjectId nor undefined, but an object containing _id which is an ObjectId.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jonathan@mowin.se Jonathan Nielsen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: