{"record":{"id":"a503f39a368cb067","repo":"mongodb/node-mongodb-native","slug":"parent-provided-to-changestream-constructor-must-b","errorCode":null,"errorMessage":"Parent provided to ChangeStream constructor must be an instance of Collection, Db, or MongoClient","messagePattern":"Parent provided to ChangeStream constructor must be an instance of Collection, Db, or MongoClient","errorType":"exception","errorClass":"MongoChangeStreamError","httpStatus":null,"severity":"error","filePath":"src/change_stream.ts","lineNumber":663,"sourceCode":"  ) {\n    super();\n\n    this.pipeline = pipeline;\n    this.options = { ...options };\n    let serverSelectionTimeoutMS: number;\n    delete this.options.writeConcern;\n\n    if (parent instanceof Collection) {\n      this.type = CHANGE_DOMAIN_TYPES.COLLECTION;\n      serverSelectionTimeoutMS = parent.s.db.client.options.serverSelectionTimeoutMS;\n    } else if (parent instanceof Db) {\n      this.type = CHANGE_DOMAIN_TYPES.DATABASE;\n      serverSelectionTimeoutMS = parent.client.options.serverSelectionTimeoutMS;\n    } else if (parent instanceof MongoClient) {\n      this.type = CHANGE_DOMAIN_TYPES.CLUSTER;\n      serverSelectionTimeoutMS = parent.options.serverSelectionTimeoutMS;\n    } else {\n      throw new MongoChangeStreamError(\n        'Parent provided to ChangeStream constructor must be an instance of Collection, Db, or MongoClient'\n      );\n    }\n\n    this.contextOwner = Symbol();\n    this.parent = parent;\n    this.namespace = parent.s.namespace;\n    if (!this.options.readPreference && parent.readPreference) {\n      this.options.readPreference = parent.readPreference;\n    }\n\n    // Create contained Change Stream cursor\n    this.cursor = this._createChangeStreamCursor(options);\n\n    this.isClosed = false;\n    this.mode = false;\n\n    // Listen for any `change` listeners being added to ChangeStream","sourceCodeStart":645,"sourceCodeEnd":681,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/change_stream.ts#L645-L681","documentation":"Thrown by the ChangeStream constructor when the parent argument is not an instance of Collection, Db, or MongoClient. The driver uses the parent type to determine the change stream's domain (collection-level, database-level, or cluster-level) and to access the underlying MongoClient. This is a MongoChangeStreamError.","triggerScenarios":"Directly instantiating new ChangeStream(parent, pipeline, options) where parent is something other than a Collection, Db, or MongoClient instance (e.g., a raw topology, a cursor, or null). The normal entry points (collection.watch(), db.watch(), client.watch()) always pass a valid parent, so this only occurs with direct construction.","commonSituations":"Subclassing or wrapping ChangeStream and passing a mock or proxy object; copying example code that manually constructs a ChangeStream; library or framework code that abstracts over the driver and passes the wrong object type.","solutions":["Do not construct ChangeStream directly; use collection.watch(), db.watch(), or client.watch() instead","If you must construct directly, ensure the parent is a genuine Collection, Db, or MongoClient instance (not a wrapper or mock)","Check parent instanceof Collection || parent instanceof Db || parent instanceof MongoClient before construction"],"exampleFix":"// before\nconst stream = new ChangeStream(someTopologyObject, pipeline);\n\n// after\nconst stream = collection.watch(pipeline);\n// or: db.watch(pipeline)\n// or: client.watch(pipeline)","handlingStrategy":"validation","validationCode":"// Before constructing a ChangeStream directly\nimport { Collection, Db, MongoClient } from 'mongodb';\nif (!(parent instanceof Collection || parent instanceof Db || parent instanceof MongoClient)) {\n  throw new TypeError('parent must be Collection, Db, or MongoClient');\n}","typeGuard":"import type { Collection, Db, MongoClient } from 'mongodb';\nfunction isValidChangeStreamParent(parent: unknown): parent is Collection | Db | MongoClient {\n  return parent instanceof Collection || parent instanceof Db || parent instanceof MongoClient;\n}","tryCatchPattern":null,"preventionTips":["Never construct ChangeStream directly; always use collection.watch(), db.watch(), or client.watch()","If wrapping the driver, validate the parent type before passing it to ChangeStream"],"tags":["change-stream","validation","api-misuse"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}