Skip to content

Commit 7d8d00a

Browse files
committed
tests and docs
1 parent a46d35d commit 7d8d00a

File tree

6 files changed

+102
-8
lines changed

6 files changed

+102
-8
lines changed

src/bulk/common.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface DeleteManyModel<TSchema extends Document = Document> {
6969

7070
/** @public */
7171
export interface ReplaceOneModel<TSchema extends Document = Document> {
72-
/** The filter to limit the replaced document. */
72+
/** The filter that specifies which document to replace. In the case of multiple matches, the first document matched is replaced. */
7373
filter: Filter<TSchema>;
7474
/** The document with which to replace the matched document. */
7575
replacement: WithoutId<TSchema>;
@@ -79,13 +79,13 @@ export interface ReplaceOneModel<TSchema extends Document = Document> {
7979
hint?: Hint;
8080
/** When true, creates a new document if no document matches the query. */
8181
upsert?: boolean;
82-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
82+
/** Specifies the sort order for the documents matched by the filter. */
8383
sort?: Sort;
8484
}
8585

8686
/** @public */
8787
export interface UpdateOneModel<TSchema extends Document = Document> {
88-
/** The filter to limit the updated documents. */
88+
/** The filter that specifies which document to update. In the case of multiple matches, the first document matched is updated. */
8989
filter: Filter<TSchema>;
9090
/**
9191
* The modifications to apply. The value can be either:
@@ -101,7 +101,7 @@ export interface UpdateOneModel<TSchema extends Document = Document> {
101101
hint?: Hint;
102102
/** When true, creates a new document if no document matches the query. */
103103
upsert?: boolean;
104-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
104+
/** Specifies the sort order for the documents matched by the filter. */
105105
sort?: Sort;
106106
}
107107

src/operations/client_bulk_write/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export interface ClientReplaceOneModel<TSchema> extends ClientWriteModel {
9090
hint?: Hint;
9191
/** When true, creates a new document if no document matches the query. */
9292
upsert?: boolean;
93-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
93+
/** Specifies the sort order for the documents matched by the filter. */
9494
sort?: Sort;
9595
}
9696

@@ -116,7 +116,7 @@ export interface ClientUpdateOneModel<TSchema> extends ClientWriteModel {
116116
hint?: Hint;
117117
/** When true, creates a new document if no document matches the query. */
118118
upsert?: boolean;
119-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
119+
/** Specifies the sort order for the documents matched by the filter. */
120120
sort?: Sort;
121121
}
122122

src/operations/update.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface UpdateStatement {
5959
arrayFilters?: Document[];
6060
/** A document or string that specifies the index to use to support the query predicate. */
6161
hint?: Hint;
62-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
62+
/** Specifies the sort order for the documents matched by the filter. */
6363
sort?: SortForCmd;
6464
}
6565

@@ -217,7 +217,7 @@ export interface ReplaceOptions extends CommandOperationOptions {
217217
upsert?: boolean;
218218
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
219219
let?: Document;
220-
/** if the filter matches more than one document the first one matched by the sort order will be updated */
220+
/** Specifies the sort order for the documents matched by the filter. */
221221
sort?: Sort;
222222
}
223223

test/integration/crud/client_bulk_write.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,35 @@ describe('Client Bulk Write', function () {
396396
});
397397
});
398398
});
399+
400+
describe('sort support', () => {
401+
describe('updateMany does not support sort option', function () {
402+
const commands: CommandStartedEvent[] = [];
403+
404+
beforeEach(async function () {
405+
client = this.configuration.newClient({}, { monitorCommands: true });
406+
407+
client.on('commandStarted', filterForCommands('bulkWrite', commands));
408+
await client.connect();
409+
});
410+
411+
it('should not include sort field in the command', async function () {
412+
await client.bulkWrite([
413+
{
414+
name: 'updateMany',
415+
namespace: 'foo.bar',
416+
filter: { age: { $lte: 5 } },
417+
update: { $set: { puppy: true } },
418+
// @ts-expect-error: sort is not supported in updateMany
419+
sort: { age: 1 } // This sort option should be ignored
420+
}
421+
]);
422+
423+
expect(commands).to.have.lengthOf(1);
424+
const [updateCommand] = commands;
425+
expect(updateCommand.commandName).to.equal('bulkWrite');
426+
expect(updateCommand.command.ops[0]).to.not.have.property('sort');
427+
});
428+
});
429+
});
399430
});

test/integration/crud/crud_api.test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { finished } from 'stream/promises';
66
import {
77
Collection,
88
CommandFailedEvent,
9+
type CommandStartedEvent,
910
CommandSucceededEvent,
1011
MongoBulkWriteError,
1112
type MongoClient,
@@ -1152,4 +1153,53 @@ describe('CRUD API', function () {
11521153
});
11531154
}
11541155
});
1156+
1157+
describe('sort support', function () {
1158+
let client: MongoClient;
1159+
let events: Array<CommandStartedEvent>;
1160+
let collection: Collection;
1161+
1162+
beforeEach(async function () {
1163+
client = this.configuration.newClient({ monitorCommands: true });
1164+
events = [];
1165+
client.on('commandStarted', commandStarted =>
1166+
commandStarted.commandName === 'update' ? events.push(commandStarted) : null
1167+
);
1168+
1169+
collection = client.db('updateManyTest').collection('updateManyTest');
1170+
await collection.drop().catch(() => null);
1171+
await collection.insertMany([{ a: 1 }, { a: 2 }]);
1172+
});
1173+
1174+
afterEach(async function () {
1175+
await collection.drop().catch(() => null);
1176+
await client.close();
1177+
});
1178+
1179+
describe('collection.updateMany()', () => {
1180+
it('does not attach a sort property if one is specified', async function () {
1181+
// @ts-expect-error: sort is not supported
1182+
await collection.updateMany({ a: { $gte: 1 } }, { $set: { b: 1 } }, { sort: { a: 1 } });
1183+
1184+
expect(events).to.have.lengthOf(1);
1185+
const [updateEvent] = events;
1186+
expect(updateEvent.commandName).to.equal('update');
1187+
expect(updateEvent.command.updates[0]).to.not.have.property('sort');
1188+
});
1189+
});
1190+
1191+
describe('collection.bulkWrite([{updateMany}])', () => {
1192+
it('does not attach a sort property if one is specified', async function () {
1193+
await collection.bulkWrite([
1194+
// @ts-expect-error: sort is not supported
1195+
{ updateMany: { filter: { a: { $gte: 1 } }, update: { $set: { b: 1 } }, sort: { a: 1 } } }
1196+
]);
1197+
1198+
expect(events).to.have.lengthOf(1);
1199+
const [updateEvent] = events;
1200+
expect(updateEvent.commandName).to.equal('update');
1201+
expect(updateEvent.command.updates[0]).to.not.have.property('sort');
1202+
});
1203+
});
1204+
});
11551205
});

test/types/sort.test-d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import {
88
type Sort
99
} from '../mongodb';
1010

11+
const sortFieldName: Sort = 'a';
12+
const sortFieldNameObject: Sort = { a: 1, b: -1 };
13+
const sortFieldNameList: Sort = ['a', 'b'];
14+
const sortFieldNameTuple: Sort = ['a', 1];
15+
const sortFieldNameTuples: Sort = [
16+
['a', 1],
17+
['b', -1]
18+
];
19+
const sortFieldNameMap: Sort = new Map([
20+
['a', 1],
21+
['b', -1]
22+
]);
23+
1124
const sorts = [
1225
// field names
1326
'a',

0 commit comments

Comments
 (0)