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
6 changes: 3 additions & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
update: UpdateFilter<TSchema> | Partial<TSchema>,
options?: UpdateOptions
): Promise<UpdateResult> {
): Promise<UpdateResult<TSchema>> {
return executeOperation(
this.s.db.s.client,
new UpdateOneOperation(
Expand All @@ -357,7 +357,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options?: ReplaceOptions
): Promise<UpdateResult | Document> {
): Promise<UpdateResult<TSchema> | Document> {
return executeOperation(
this.s.db.s.client,
new ReplaceOneOperation(
Expand All @@ -380,7 +380,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options?: UpdateOptions
): Promise<UpdateResult> {
): Promise<UpdateResult<TSchema>> {
return executeOperation(
this.s.db.s.client,
new UpdateManyOperation(
Expand Down
12 changes: 8 additions & 4 deletions src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Document, ObjectId } from '../bson';
import type { Document } from '../bson';
import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
import type { InferIdType } from '../mongo_types';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { Callback, hasAtomicOperators, MongoDBNamespace } from '../utils';
Expand All @@ -23,8 +24,11 @@ export interface UpdateOptions extends CommandOperationOptions {
let?: Document;
}

/** @public */
export interface UpdateResult {
/**
* @public
* `TSchema` is the schema of the collection
*/
export interface UpdateResult<TSchema extends Document = Document> {
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
acknowledged: boolean;
/** The number of documents that matched the filter */
Expand All @@ -34,7 +38,7 @@ export interface UpdateResult {
/** The number of documents that were upserted */
upsertedCount: number;
/** The identifier of the inserted document if an upsert took place */
upsertedId: ObjectId;
upsertedId: InferIdType<TSchema> | null;
}

/** @public */
Expand Down
23 changes: 23 additions & 0 deletions test/types/community/collection/updateX.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expectAssignable, expectError, expectNotAssignable, expectNotType } fro
import type {
AddToSetOperators,
ArrayOperator,
Collection,
MatchKeysAndValues,
PullAllOperator,
PullOperator,
Expand Down Expand Up @@ -423,3 +424,25 @@ export async function testPushWithId(): Promise<void> {
}
);
}

{
// NODE-5171 - UpdateResult is generic over the collection schema and infers the id type
const collection = {} as any as Collection;
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateOne({}, {}));
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateMany({}, {}));

expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateOne({}, {}));
expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateMany({}, {}));

const collectionWithSchema = {} as any as Collection<{ _id: number }>;
expectAssignable<Promise<{ upsertedId: number | null }>>(
collectionWithSchema.updateOne({ _id: 1234 }, {})
);
expectAssignable<Promise<{ upsertedId: number | null }>>(collectionWithSchema.updateMany({}, {}));
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
collectionWithSchema.updateOne({ _id: 1234 }, {})
);
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
collectionWithSchema.updateMany({}, {})
);
}