Skip to content

Commit b6c6734

Browse files
authored
chore(shell-api): add test and type updates for sort (#2520)
1 parent 0a147f0 commit b6c6734

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

packages/service-provider-core/src/writable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export default interface Writable {
319319
collection: string,
320320
filter: Document,
321321
update: Document,
322-
options?: UpdateOptions,
322+
options?: UpdateOptions & { sort?: Document },
323323
dbOptions?: DbOptions
324324
): Promise<UpdateResult>;
325325

packages/shell-api/src/collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ export class Collection<
930930
* @param {Object} filter - The filter.
931931
* @param {Object} replacement - The replacement document for matches.
932932
* @param {Object} options - The replace options.
933-
* <upsert, writeConcern, collation, hint>
933+
* <upsert, writeConcern, collation, hint, sort>
934934
*
935935
* @returns {UpdateResult} The promise of the result.
936936
*/
@@ -1058,7 +1058,7 @@ export class Collection<
10581058
* @param {Object} filter - The filter.
10591059
* @param {(Object|Array)} update - The updates.
10601060
* @param {Object} options - The update options.
1061-
* <upsert, writeConcern, collation, arrayFilters, hint>
1061+
* <upsert, writeConcern, collation, arrayFilters, hint, sort>
10621062
*
10631063
* @returns {UpdateResult} The promise of the result.
10641064
*/
@@ -1068,7 +1068,7 @@ export class Collection<
10681068
async updateOne(
10691069
filter: Document,
10701070
update: Document,
1071-
options: UpdateOptions = {}
1071+
options: UpdateOptions & { sort?: Document } = {}
10721072
): Promise<UpdateResult | Document> {
10731073
assertArgsDefinedType([filter], [true], 'Collection.updateOne');
10741074
this._emitCollectionApiCall('updateOne', { filter, options });

packages/shell-api/src/integration.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,61 @@ describe('Shell API (integration)', function () {
490490
});
491491
});
492492

493+
describe('updateOne and replaceOne with sort option', function () {
494+
skipIfServerVersion(testServer, '< 8.0');
495+
496+
beforeEach(async function () {
497+
await serviceProvider.insertMany(dbName, collectionName, [
498+
{ _id: 1, category: 'A', score: 20, order: 1 },
499+
{ _id: 2, category: 'A', score: 10, order: 2 },
500+
{ _id: 3, category: 'A', score: 15, order: 3 },
501+
{ _id: 4, category: 'B', score: 25, order: 1 },
502+
]);
503+
});
504+
505+
it('updates the first document based on sort order', async function () {
506+
const result = await collection.updateOne(
507+
{ category: 'A' },
508+
{ $set: { updated: true } },
509+
{ sort: { score: 1 } }
510+
);
511+
512+
expect(result.matchedCount).to.equal(1);
513+
expect(result.modifiedCount).to.equal(1);
514+
515+
// Should have updated the document with the lowest score (_id: 2, score: 10)
516+
const categoryDocs = await serviceProvider
517+
.find(dbName, collectionName, { category: 'A' })
518+
.toArray();
519+
const updatedDocs = categoryDocs.filter((doc) => doc.updated);
520+
expect(updatedDocs).to.have.lengthOf(1);
521+
expect(updatedDocs[0]._id).to.equal(2);
522+
expect(updatedDocs[0].score).to.equal(10);
523+
});
524+
525+
it('replaces the first document based on sort order', async function () {
526+
const result = await collection.replaceOne(
527+
{ category: 'A' },
528+
{ replaced: true, category: 'A' },
529+
{ sort: { score: 1 } }
530+
);
531+
532+
expect(result.matchedCount).to.equal(1);
533+
expect(result.modifiedCount).to.equal(1);
534+
535+
// Should have replaced the document with the lowest score (_id: 2, score: 10)
536+
const categoryDocs = await serviceProvider
537+
.find(dbName, collectionName, { category: 'A' })
538+
.toArray();
539+
const updatedDocs = categoryDocs.filter((doc) => doc.replaced);
540+
expect(updatedDocs).to.have.lengthOf(1);
541+
expect(updatedDocs[0]._id).to.equal(2);
542+
expect(updatedDocs[0].replaced).to.be.true;
543+
expect(updatedDocs[0].score).to.undefined;
544+
expect(updatedDocs[0].order).to.undefined;
545+
});
546+
});
547+
493548
describe('convertToCapped', function () {
494549
skipIfApiStrict();
495550
let result: Document;

0 commit comments

Comments
 (0)