diff --git a/src/collection.ts b/src/collection.ts index 60715d9f38..21c6c70dfc 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -334,7 +334,7 @@ export class Collection { filter: Filter, update: UpdateFilter | Partial, options?: UpdateOptions - ): Promise { + ): Promise> { return executeOperation( this.s.db.s.client, new UpdateOneOperation( @@ -357,7 +357,7 @@ export class Collection { filter: Filter, replacement: WithoutId, options?: ReplaceOptions - ): Promise { + ): Promise | Document> { return executeOperation( this.s.db.s.client, new ReplaceOneOperation( @@ -380,7 +380,7 @@ export class Collection { filter: Filter, update: UpdateFilter, options?: UpdateOptions - ): Promise { + ): Promise> { return executeOperation( this.s.db.s.client, new UpdateManyOperation( diff --git a/src/operations/update.ts b/src/operations/update.ts index 278a0b34b2..88b7480691 100644 --- a/src/operations/update.ts +++ b/src/operations/update.ts @@ -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'; @@ -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 { /** 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 */ @@ -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 | null; } /** @public */ diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index 9107db45ad..6bd631f53a 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -3,6 +3,7 @@ import { expectAssignable, expectError, expectNotAssignable, expectNotType } fro import type { AddToSetOperators, ArrayOperator, + Collection, MatchKeysAndValues, PullAllOperator, PullOperator, @@ -423,3 +424,25 @@ export async function testPushWithId(): Promise { } ); } + +{ + // NODE-5171 - UpdateResult is generic over the collection schema and infers the id type + const collection = {} as any as Collection; + expectAssignable>(collection.updateOne({}, {})); + expectAssignable>(collection.updateMany({}, {})); + + expectNotAssignable>(collection.updateOne({}, {})); + expectNotAssignable>(collection.updateMany({}, {})); + + const collectionWithSchema = {} as any as Collection<{ _id: number }>; + expectAssignable>( + collectionWithSchema.updateOne({ _id: 1234 }, {}) + ); + expectAssignable>(collectionWithSchema.updateMany({}, {})); + expectNotAssignable>( + collectionWithSchema.updateOne({ _id: 1234 }, {}) + ); + expectNotAssignable>( + collectionWithSchema.updateMany({}, {}) + ); +}