{"id":"10bef45c7abf8fbb","repo":"mongodb/node-mongodb-native","slug":"bson-element-name-is-missing","errorCode":null,"errorMessage":"BSON element \"${name}\" is missing","messagePattern":"BSON element \"(.+?)\" is missing","errorType":"exception","errorClass":"BSONError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/on_demand/document.ts","lineNumber":286,"sourceCode":"    required?: boolean\n  ): JSTypeOf[T] | null;\n\n  /** `required` will make `get` throw if name does not exist or is null/undefined */\n  public get<const T extends keyof JSTypeOf>(\n    name: string | number,\n    as: T,\n    required: true\n  ): JSTypeOf[T];\n\n  public get<const T extends keyof JSTypeOf>(\n    name: string | number,\n    as: T,\n    required?: boolean\n  ): JSTypeOf[T] | null {\n    const element = this.getElement(name);\n    if (element == null) {\n      if (required === true) {\n        throw new BSONError(`BSON element \"${name}\" is missing`);\n      } else {\n        return null;\n      }\n    }\n\n    if (element.value == null) {\n      const value = this.toJSValue(element.element, as);\n      if (value == null) {\n        if (required === true) {\n          throw new BSONError(`BSON element \"${name}\" is missing`);\n        } else {\n          return null;\n        }\n      }\n      // It is important to never store null\n      element.value = value;\n    }\n","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/on_demand/document.ts#L268-L304","documentation":"Thrown by OnDemandDocument.get(name, as, required) when required is true and no element with the given name exists in the document (getElement returned null). This is the on-demand BSON decoder's way of enforcing a required field. Surfaced as BSONError. It indicates the parsed BSON document is missing a field the caller declared mandatory.","triggerScenarios":"get('field', BSONType.x, true) is called on an OnDemandDocument that has no element named 'field'. In the driver this is used internally to read mandatory fields from server responses (cursor.id, ns, batch, etc.). Triggered when a server response is missing a field the driver expects, e.g. a malformed cursor document without an id.","commonSituations":"Server responses that omit a mandatory field due to a server bug or version mismatch. Talking to a non-MongoDB server that returns a partially-shaped response. Proxy/intermediary that strips fields. The error message names the missing field to aid diagnosis.","solutions":["Inspect the named field in the error; confirm the server is a genuine MongoDB instance returning the expected response shape.","Upgrade the driver and server to compatible versions.","Remove any intermediary that may rewrite server responses.","If the field is legitimately optional, the caller (driver internals) must be fixed; report a driver bug."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"import { BSONError } from 'bson';\ntry {\n  await cursor.next();\n} catch (err) {\n  if (err instanceof BSONError && /BSON element .* is missing/.test(err.message)) {\n    // a mandatory field is absent from the server response; check server/proxy\n  }\n  throw err;\n}","preventionTips":["Use a conformant MongoDB server and compatible driver version.","Remove intermediaries that strip response fields.","Report persistent occurrences with the field name from the error."],"tags":["bson","decoding","missing-field","server-response"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}