-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
update.ts
50 lines (47 loc) 路 1.17 KB
/
update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Document, ObjectId } from "../../../deps.ts";
import { UpdateOptions } from "../../types.ts";
import { WireProtocol } from "../../protocol/mod.ts";
interface UpdateResponse {
ok: number;
nModified: number;
n: number;
upserted?: {
index: number;
_id: ObjectId;
}[];
}
export async function update(
protocol: WireProtocol,
dbName: string,
collectionName: string,
query: Document,
doc: Document,
options?: UpdateOptions,
) {
const { n, nModified, upserted } = await protocol.commandSingle<
UpdateResponse
>(dbName, {
update: collectionName,
updates: [
{
q: query,
u: doc,
upsert: options?.upsert ?? false,
multi: options?.multi ?? true,
collation: options?.collation,
arrayFilters: options?.arrayFilters,
hint: options?.hint,
},
],
writeConcern: options?.writeConcern,
ordered: options?.ordered ?? true,
bypassDocumentValidation: options?.bypassDocumentValidation,
comment: options?.comment,
});
return {
upsertedIds: upserted?.map((id) => id._id),
upsertedCount: upserted?.length ?? 0,
modifiedCount: nModified,
matchedCount: n,
};
}