Skip to content

Commit d9c2600

Browse files
authored
feat(NODE-5241): add option to return modified document (#3710)
1 parent dc464c6 commit d9c2600

File tree

8 files changed

+795
-667
lines changed

8 files changed

+795
-667
lines changed

src/collection.ts

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ import {
100100
} from './utils';
101101
import { WriteConcern, type WriteConcernOptions } from './write_concern';
102102

103-
/**
104-
* @public
105-
* @deprecated This type will be completely removed and findOneAndUpdate,
106-
* findOneAndDelete, and findOneAndReplace will then return the
107-
* actual result document.
108-
*/
103+
/** @public */
109104
export interface ModifyResult<TSchema = Document> {
110105
value: WithId<TSchema> | null;
111106
lastErrorObject?: Document;
@@ -825,10 +820,23 @@ export class Collection<TSchema extends Document = Document> {
825820
* @param filter - The filter used to select the document to remove
826821
* @param options - Optional settings for the command
827822
*/
823+
async findOneAndDelete(
824+
filter: Filter<TSchema>,
825+
options: FindOneAndDeleteOptions & { includeResultMetadata: true }
826+
): Promise<ModifyResult<TSchema>>;
827+
async findOneAndDelete(
828+
filter: Filter<TSchema>,
829+
options: FindOneAndDeleteOptions & { includeResultMetadata: false }
830+
): Promise<WithId<TSchema> | null>;
831+
async findOneAndDelete(
832+
filter: Filter<TSchema>,
833+
options: FindOneAndDeleteOptions
834+
): Promise<ModifyResult<TSchema>>;
835+
async findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
828836
async findOneAndDelete(
829837
filter: Filter<TSchema>,
830838
options?: FindOneAndDeleteOptions
831-
): Promise<ModifyResult<TSchema>> {
839+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
832840
return executeOperation(
833841
this.client,
834842
new FindOneAndDeleteOperation(
@@ -846,11 +854,30 @@ export class Collection<TSchema extends Document = Document> {
846854
* @param replacement - The Document that replaces the matching document
847855
* @param options - Optional settings for the command
848856
*/
857+
async findOneAndReplace(
858+
filter: Filter<TSchema>,
859+
replacement: WithoutId<TSchema>,
860+
options: FindOneAndReplaceOptions & { includeResultMetadata: true }
861+
): Promise<ModifyResult<TSchema>>;
862+
async findOneAndReplace(
863+
filter: Filter<TSchema>,
864+
replacement: WithoutId<TSchema>,
865+
options: FindOneAndReplaceOptions & { includeResultMetadata: false }
866+
): Promise<WithId<TSchema> | null>;
867+
async findOneAndReplace(
868+
filter: Filter<TSchema>,
869+
replacement: WithoutId<TSchema>,
870+
options: FindOneAndReplaceOptions
871+
): Promise<ModifyResult<TSchema>>;
872+
async findOneAndReplace(
873+
filter: Filter<TSchema>,
874+
replacement: WithoutId<TSchema>
875+
): Promise<ModifyResult<TSchema>>;
849876
async findOneAndReplace(
850877
filter: Filter<TSchema>,
851878
replacement: WithoutId<TSchema>,
852879
options?: FindOneAndReplaceOptions
853-
): Promise<ModifyResult<TSchema>> {
880+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
854881
return executeOperation(
855882
this.client,
856883
new FindOneAndReplaceOperation(
@@ -869,11 +896,30 @@ export class Collection<TSchema extends Document = Document> {
869896
* @param update - Update operations to be performed on the document
870897
* @param options - Optional settings for the command
871898
*/
899+
async findOneAndUpdate(
900+
filter: Filter<TSchema>,
901+
update: UpdateFilter<TSchema>,
902+
options: FindOneAndUpdateOptions & { includeResultMetadata: true }
903+
): Promise<ModifyResult<TSchema>>;
904+
async findOneAndUpdate(
905+
filter: Filter<TSchema>,
906+
update: UpdateFilter<TSchema>,
907+
options: FindOneAndUpdateOptions & { includeResultMetadata: false }
908+
): Promise<WithId<TSchema> | null>;
909+
async findOneAndUpdate(
910+
filter: Filter<TSchema>,
911+
update: UpdateFilter<TSchema>,
912+
options: FindOneAndUpdateOptions
913+
): Promise<ModifyResult<TSchema>>;
914+
async findOneAndUpdate(
915+
filter: Filter<TSchema>,
916+
update: UpdateFilter<TSchema>
917+
): Promise<ModifyResult<TSchema>>;
872918
async findOneAndUpdate(
873919
filter: Filter<TSchema>,
874920
update: UpdateFilter<TSchema>,
875921
options?: FindOneAndUpdateOptions
876-
): Promise<ModifyResult<TSchema>> {
922+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
877923
return executeOperation(
878924
this.client,
879925
new FindOneAndUpdateOperation(

src/operations/find_and_modify.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface FindOneAndDeleteOptions extends CommandOperationOptions {
2929
sort?: Sort;
3030
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
3131
let?: Document;
32+
/** Return the raw result document instead of the ModifyResult */
33+
includeResultMetadata?: boolean;
3234
}
3335

3436
/** @public */
@@ -47,6 +49,8 @@ export interface FindOneAndReplaceOptions extends CommandOperationOptions {
4749
upsert?: boolean;
4850
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
4951
let?: Document;
52+
/** Return the raw result document instead of the ModifyResult */
53+
includeResultMetadata?: boolean;
5054
}
5155

5256
/** @public */
@@ -67,6 +71,8 @@ export interface FindOneAndUpdateOptions extends CommandOperationOptions {
6771
upsert?: boolean;
6872
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
6973
let?: Document;
74+
/** Return the raw result document instead of the ModifyResult */
75+
includeResultMetadata?: boolean;
7076
}
7177

7278
/** @internal */
@@ -127,6 +133,8 @@ class FindAndModifyOperation extends CommandOperation<Document> {
127133
upsert: false
128134
};
129135

136+
options.includeResultMetadata ??= true;
137+
130138
const sort = formatSort(options.sort);
131139
if (sort) {
132140
this.cmdBase.sort = sort;
@@ -205,7 +213,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
205213
// Execute the command
206214
super.executeCommand(server, session, cmd, (err, result) => {
207215
if (err) return callback(err);
208-
return callback(undefined, result);
216+
return callback(undefined, options.includeResultMetadata ? result : result.value ?? null);
209217
});
210218
}
211219
}

0 commit comments

Comments
 (0)