{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/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. A change stream must be rooted in one of these objects because the constructor derives the namespace, read preference, and server-selection timeout from the parent. This error usually surfaces when users try to instantiate ChangeStream directly instead of calling collection.watch()/db.watch()/client.watch().","triggerScenarios":"Calling new ChangeStream(someObject) directly where someObject is a plain object, a Promise, a cursor, or some other driver type (e.g. a Session or a Bulk). Passing a collection-like object from a different driver version or a mock.","commonSituations":"Users manually constructing a ChangeStream rather than using the watch() helper. Mocking in tests with objects that lack the proper prototype. Version skew where Collection is imported from two different driver versions (instanceof fails across module copies).","solutions":["Use the public entry points: collection.watch(), db.watch(), or client.watch() instead of new ChangeStream().","If mocking in tests, either instantiate a real Collection stub with the correct prototype or use a fake-timer/test double at the watch() boundary.","Resolve duplicate driver installations (npm ls mongodb) so instanceof checks use the same class definition."],"exampleFix":"// before\nconst stream = new ChangeStream(myPlainObject, pipeline, options);\n\n// after\nconst stream = collection.watch(pipeline, options);","handlingStrategy":"type-guard","validationCode":"import { Collection, Db, MongoClient } from 'mongodb';\n\nfunction assertChangeStreamParent(parent) {\n  if (!(parent instanceof Collection || parent instanceof Db || parent instanceof MongoClient)) {\n    throw new TypeError('parent must be Collection, Db, or MongoClient');\n  }\n}","typeGuard":"function isChangeStreamParent(parent) {\n  return parent instanceof Collection || parent instanceof Db || parent instanceof MongoClient;\n}","tryCatchPattern":"try {\n  stream = parent.watch(pipeline, options);\n} catch (e) {\n  if (e instanceof MongoChangeStreamError && /Collection, Db, or MongoClient/.test(e.message)) {\n    // obtain a real Collection/Db/MongoClient before retrying\n  }\n}","preventionTips":["Never call new ChangeStream() directly; use collection.watch()/db.watch()/client.watch().","Resolve duplicate mongodb installations (npm ls mongodb) so instanceof uses one class.","In tests, stub at the watch() boundary rather than passing fake objects into the constructor."],"tags":["change-stream","constructor","argument-error","instanceof"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}