Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
);
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ export class Collection<TSchema extends Document = Document> {
/** @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,
Expand Down Expand Up @@ -228,7 +234,7 @@ export class Collection<TSchema extends Document = Document> {
*/
get readConcern(): ReadConcern | undefined {
if (this.s.readConcern == null) {
return this.s.db.readConcern;
return this.db.readConcern;
}
return this.s.readConcern;
}
Expand All @@ -239,7 +245,7 @@ export class Collection<TSchema extends Document = Document> {
*/
get readPreference(): ReadPreference | undefined {
if (this.s.readPreference == null) {
return this.s.db.readPreference;
return this.db.readPreference;
}

return this.s.readPreference;
Expand All @@ -255,7 +261,7 @@ export class Collection<TSchema extends Document = Document> {
*/
get writeConcern(): WriteConcern | undefined {
if (this.s.writeConcern == null) {
return this.s.db.writeConcern;
return this.db.writeConcern;
}
return this.s.writeConcern;
}
Expand Down Expand Up @@ -509,7 +515,7 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
*/
async drop(options?: DropCollectionOptions): Promise<boolean> {
return await this.s.db.dropCollection(this.collectionName, options);
return await this.db.dropCollection(this.collectionName, options);
}

/**
Expand Down Expand Up @@ -578,7 +584,7 @@ export class Collection<TSchema extends Document = Document> {
*/
async options(options?: OperationOptions): Promise<Document> {
options = resolveOptions(this, options);
const [collection] = await this.s.db
const [collection] = await this.db
.listCollections({ name: this.collectionName }, { ...options, nameOnly: false })
.toArray();

Expand Down
5 changes: 4 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/operations/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class RenameOperation extends CommandOperation<Document> {
}

override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down