Skip to content

feat(NODE-5241): add option to return modified document #3710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 21, 2023
64 changes: 55 additions & 9 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ import {
} from './utils';
import { WriteConcern, type WriteConcernOptions } from './write_concern';

/**
* @public
* @deprecated This type will be completely removed and findOneAndUpdate,
* findOneAndDelete, and findOneAndReplace will then return the
* actual result document.
*/
/** @public */
export interface ModifyResult<TSchema = Document> {
value: WithId<TSchema> | null;
lastErrorObject?: Document;
Expand Down Expand Up @@ -825,10 +820,23 @@ export class Collection<TSchema extends Document = Document> {
* @param filter - The filter used to select the document to remove
* @param options - Optional settings for the command
*/
async findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions & { includeResultMetadata: true }
): Promise<ModifyResult<TSchema>>;
async findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions & { includeResultMetadata: false }
): Promise<WithId<TSchema> | null>;
async findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema>>;
async findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
async findOneAndDelete(
filter: Filter<TSchema>,
options?: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema>> {
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
return executeOperation(
this.client,
new FindOneAndDeleteOperation(
Expand All @@ -846,11 +854,30 @@ export class Collection<TSchema extends Document = Document> {
* @param replacement - The Document that replaces the matching document
* @param options - Optional settings for the command
*/
async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndReplaceOptions & { includeResultMetadata: true }
): Promise<ModifyResult<TSchema>>;
async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndReplaceOptions & { includeResultMetadata: false }
): Promise<WithId<TSchema> | null>;
async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndReplaceOptions
): Promise<ModifyResult<TSchema>>;
async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>
): Promise<ModifyResult<TSchema>>;
async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options?: FindOneAndReplaceOptions
): Promise<ModifyResult<TSchema>> {
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
return executeOperation(
this.client,
new FindOneAndReplaceOperation(
Expand All @@ -869,11 +896,30 @@ export class Collection<TSchema extends Document = Document> {
* @param update - Update operations to be performed on the document
* @param options - Optional settings for the command
*/
async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndUpdateOptions & { includeResultMetadata: true }
): Promise<ModifyResult<TSchema>>;
async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndUpdateOptions & { includeResultMetadata: false }
): Promise<WithId<TSchema> | null>;
async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndUpdateOptions
): Promise<ModifyResult<TSchema>>;
async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>
): Promise<ModifyResult<TSchema>>;
async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options?: FindOneAndUpdateOptions
): Promise<ModifyResult<TSchema>> {
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
return executeOperation(
this.client,
new FindOneAndUpdateOperation(
Expand Down
10 changes: 9 additions & 1 deletion src/operations/find_and_modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface FindOneAndDeleteOptions extends CommandOperationOptions {
sort?: Sort;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
/** Return the raw result document instead of the ModifyResult */
includeResultMetadata?: boolean;
}

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

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

/** @internal */
Expand Down Expand Up @@ -127,6 +133,8 @@ class FindAndModifyOperation extends CommandOperation<Document> {
upsert: false
};

options.includeResultMetadata ??= true;

const sort = formatSort(options.sort);
if (sort) {
this.cmdBase.sort = sort;
Expand Down Expand Up @@ -205,7 +213,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
// Execute the command
super.executeCommand(server, session, cmd, (err, result) => {
if (err) return callback(err);
return callback(undefined, result);
return callback(undefined, options.includeResultMetadata ? result : result.value ?? null);
});
}
}
Expand Down
Loading