Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Sep 10, 2024
1 parent c3c6333 commit 86368fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
64 changes: 29 additions & 35 deletions src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@ import { MongoAPIError, MongoCompatibilityError, MongoInvalidArgumentError, Mong
import type { InferIdType, TODO_NODE_3286 } from '../mongo_types';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { formatSort, SortForCmd } from '../sort';
import { hasAtomicOperators, type MongoDBNamespace } from '../utils';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects, type Hint } from './operation';


/** @public */
export interface UpdateOneOptions extends UpdateOptions {
/**
* Specify which document the operation updates if the query matches multiple
* documents. The first document matched by the sort order will be updated.
*
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
*/
sort?: Document;
}

/** @public */
export interface UpdateOptions extends CommandOperationOptions {
/** A set of filters specifying to which array elements an update should apply */
Expand All @@ -34,6 +23,15 @@ export interface UpdateOptions extends CommandOperationOptions {
upsert?: boolean;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
/**
* Specify which document the operation updates if the query matches multiple
* documents. The first document matched by the sort order will be updated.
*
* The server will report an error if the caller explicitly provides a value with updateMany().
*
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
*/
sort?: Document;
}

/**
Expand All @@ -53,12 +51,6 @@ export interface UpdateResult<TSchema extends Document = Document> {
upsertedId: InferIdType<TSchema> | null;
}

/** @public */
export interface UpdateOneStatement extends UpdateStatement {
/** If the query matches multiple documents, the first document matched by the sort order will be updated. */
sort?: Document;
}

/** @public */
export interface UpdateStatement {
/** The query that matches documents to update. */
Expand All @@ -75,6 +67,8 @@ export interface UpdateStatement {
arrayFilters?: Document[];
/** A document or string that specifies the index to use to support the query predicate. */
hint?: Hint;
/** If the query matches multiple documents, the first document matched by the sort order will be updated. */
sort?: SortForCmd;
}

/**
Expand Down Expand Up @@ -147,7 +141,7 @@ export class UpdateOperation extends CommandOperation<Document> {

/** @internal */
export class UpdateOneOperation extends UpdateOperation {
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOneOptions) {
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOptions) {
super(
collection.s.namespace,
[makeUpdateStatement(filter, update, { ...options, multi: false })],
Expand Down Expand Up @@ -213,17 +207,6 @@ export class UpdateManyOperation extends UpdateOperation {
}
}

/** @public */
export interface ReplaceOneOptions extends ReplaceOptions {
/**
* Specify which document the operation replaces if the query matches multiple
* documents. The first document matched by the sort order will be replaced.
*
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
*/
sort?: Document;
}

/** @public */
export interface ReplaceOptions extends CommandOperationOptions {
/** If true, allows the write to opt-out of document level validation */
Expand All @@ -236,6 +219,15 @@ export interface ReplaceOptions extends CommandOperationOptions {
upsert?: boolean;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
/**
* Specify which document the operation replaces if the query matches multiple
* documents. The first document matched by the sort order will be replaced.
*
* The server will report an error if the caller explicitly provides a value with replaceMany().
*
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
*/
sort?: Document;
}

/** @internal */
Expand All @@ -244,7 +236,7 @@ export class ReplaceOneOperation extends UpdateOperation {
collection: Collection,
filter: Document,
replacement: Document,
options: ReplaceOneOptions
options: ReplaceOptions
) {
super(
collection.s.namespace,
Expand Down Expand Up @@ -280,8 +272,8 @@ export class ReplaceOneOperation extends UpdateOperation {
export function makeUpdateStatement(
filter: Document,
update: Document | Document[],
options: UpdateOneOptions & { multi?: boolean }
): UpdateOneStatement {
options: UpdateOptions & { multi?: boolean }
): UpdateStatement {
if (filter == null || typeof filter !== 'object') {
throw new MongoInvalidArgumentError('Selector must be a valid JavaScript object');
}
Expand All @@ -297,8 +289,10 @@ export function makeUpdateStatement(

if (options.multi) {
op.multi = options.multi;
} else {
op.sort = options?.sort;
}

if (options.sort) {
op.sort = formatSort(options?.sort);
}

if (options.hint) {
Expand Down
12 changes: 8 additions & 4 deletions test/integration/crud/crud.spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ const loadBalancedCollationTests = [
'Distinct with a collation'
];

describe.only('CRUD unified', function () {
describe('CRUD unified', function () {
runUnifiedSuite(
loadSpecTests(path.join('crud', 'unified')),
({ description }, { isLoadBalanced }) => {
return description === 'BulkWrite replaceOne with sort option unsupported (server-side error)'
? false
: 'skipping you';
return description.match(clientBulkWriteTests)
? 'TODO(NODE-6257): implement client level bulk write'
: unacknowledgedHintTests.includes(description)
? `TODO(NODE-3541)`
: isLoadBalanced && loadBalancedCollationTests.includes(description)
? `TODO(NODE-6280): fix collation for find and modify commands on load balanced mode`
: false;
}
);
});

0 comments on commit 86368fe

Please sign in to comment.