diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 4b1578903ea..211c802c182 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -900,7 +900,7 @@ export abstract class BulkOperationBase { // Final options for retryable writes let finalOptions = Object.assign({}, options); - finalOptions = applyRetryableWrites(finalOptions, collection.s.db); + finalOptions = applyRetryableWrites(finalOptions, collection.db); // Final results const bulkResult: BulkResult = { @@ -1228,7 +1228,7 @@ export abstract class BulkOperationBase { private shouldForceServerObjectId(): boolean { return ( this.s.options.forceServerObjectId === true || - this.s.collection.s.db.options?.forceServerObjectId === true + this.s.collection.db.options?.forceServerObjectId === true ); } } diff --git a/src/collection.ts b/src/collection.ts index 37339881c3b..f693bdf5a47 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -172,11 +172,17 @@ export class Collection { /** @internal */ client: MongoClient; + /** + * Get the database object for the collection. + */ + readonly db: Db; + /** * Create a new Collection instance * @internal */ constructor(db: Db, name: string, options?: CollectionOptions) { + this.db = db; // Internal state this.s = { db, @@ -228,7 +234,7 @@ export class Collection { */ get readConcern(): ReadConcern | undefined { if (this.s.readConcern == null) { - return this.s.db.readConcern; + return this.db.readConcern; } return this.s.readConcern; } @@ -239,7 +245,7 @@ export class Collection { */ get readPreference(): ReadPreference | undefined { if (this.s.readPreference == null) { - return this.s.db.readPreference; + return this.db.readPreference; } return this.s.readPreference; @@ -255,7 +261,7 @@ export class Collection { */ get writeConcern(): WriteConcern | undefined { if (this.s.writeConcern == null) { - return this.s.db.writeConcern; + return this.db.writeConcern; } return this.s.writeConcern; } @@ -509,7 +515,7 @@ export class Collection { * @param options - Optional settings for the command */ async drop(options?: DropCollectionOptions): Promise { - return await this.s.db.dropCollection(this.collectionName, options); + return await this.db.dropCollection(this.collectionName, options); } /** @@ -578,7 +584,7 @@ export class Collection { */ async options(options?: OperationOptions): Promise { options = resolveOptions(this, options); - const [collection] = await this.s.db + const [collection] = await this.db .listCollections({ name: this.collectionName }, { ...options, nameOnly: false }) .toArray(); diff --git a/src/db.ts b/src/db.ts index a5c678f4f1e..54724ae31be 100644 --- a/src/db.ts +++ b/src/db.ts @@ -126,7 +126,10 @@ export class Db { /** @internal */ s: DbPrivate; - /** @internal */ + /** + * Gets the MongoClient associated with the Db. + * @public + */ readonly client: MongoClient; public static SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION; diff --git a/src/operations/rename.ts b/src/operations/rename.ts index 0ba34c2baf5..a0efb47a6d7 100644 --- a/src/operations/rename.ts +++ b/src/operations/rename.ts @@ -48,7 +48,7 @@ export class RenameOperation extends CommandOperation { } override handleOk(_response: InstanceType): Document { - return new Collection(this.collection.s.db, this.newName, this.collection.s.options); + return new Collection(this.collection.db, this.newName, this.collection.s.options); } } diff --git a/src/utils.ts b/src/utils.ts index c4a68dcd524..c6f40fb43e9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1362,7 +1362,7 @@ export function maybeAddIdToDocuments( options: { forceServerObjectId?: boolean } ): Document { const forceServerObjectId = - options.forceServerObjectId ?? collection.s.db.options?.forceServerObjectId ?? false; + options.forceServerObjectId ?? collection.db.options?.forceServerObjectId ?? false; // no need to modify the docs if server sets the ObjectId if (forceServerObjectId) {