Skip to content

Commit f76635a

Browse files
feat(NODE-3866): Add let option to ReplaceOptions for replaceOne operation (#3148)
1 parent b578d89 commit f76635a

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed

src/operations/update.ts

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ export interface ReplaceOptions extends CommandOperationOptions {
222222
hint?: string | Document;
223223
/** When true, creates a new document if no document matches the query */
224224
upsert?: boolean;
225+
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
226+
let?: Document;
225227
}
226228

227229
/** @internal */
+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
{
2+
"description": "replaceOne-let",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "crud-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "coll0"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "coll0",
31+
"databaseName": "crud-tests",
32+
"documents": [
33+
{
34+
"_id": 1
35+
},
36+
{
37+
"_id": 2
38+
}
39+
]
40+
}
41+
],
42+
"tests": [
43+
{
44+
"description": "ReplaceOne with let option",
45+
"runOnRequirements": [
46+
{
47+
"minServerVersion": "5.0"
48+
}
49+
],
50+
"operations": [
51+
{
52+
"name": "replaceOne",
53+
"object": "collection0",
54+
"arguments": {
55+
"filter": {
56+
"$expr": {
57+
"$eq": [
58+
"$_id",
59+
"$$id"
60+
]
61+
}
62+
},
63+
"replacement": {
64+
"x": "foo"
65+
},
66+
"let": {
67+
"id": 1
68+
}
69+
},
70+
"expectResult": {
71+
"matchedCount": 1,
72+
"modifiedCount": 1,
73+
"upsertedCount": 0
74+
}
75+
}
76+
],
77+
"expectEvents": [
78+
{
79+
"client": "client0",
80+
"events": [
81+
{
82+
"commandStartedEvent": {
83+
"command": {
84+
"update": "coll0",
85+
"updates": [
86+
{
87+
"q": {
88+
"$expr": {
89+
"$eq": [
90+
"$_id",
91+
"$$id"
92+
]
93+
}
94+
},
95+
"u": {
96+
"x": "foo"
97+
}
98+
}
99+
],
100+
"let": {
101+
"id": 1
102+
}
103+
}
104+
}
105+
}
106+
]
107+
}
108+
],
109+
"outcome": [
110+
{
111+
"collectionName": "coll0",
112+
"databaseName": "crud-tests",
113+
"documents": [
114+
{
115+
"_id": 1,
116+
"x": "foo"
117+
},
118+
{
119+
"_id": 2
120+
}
121+
]
122+
}
123+
]
124+
},
125+
{
126+
"description": "ReplaceOne with let option unsupported (server-side error)",
127+
"runOnRequirements": [
128+
{
129+
"minServerVersion": "3.6.0",
130+
"maxServerVersion": "4.4.99"
131+
}
132+
],
133+
"operations": [
134+
{
135+
"name": "replaceOne",
136+
"object": "collection0",
137+
"arguments": {
138+
"filter": {
139+
"$expr": {
140+
"$eq": [
141+
"$_id",
142+
"$$id"
143+
]
144+
}
145+
},
146+
"replacement": {
147+
"x": "foo"
148+
},
149+
"let": {
150+
"id": 1
151+
}
152+
},
153+
"expectError": {
154+
"errorContains": "'update.let' is an unknown field",
155+
"isClientError": false
156+
}
157+
}
158+
],
159+
"expectEvents": [
160+
{
161+
"client": "client0",
162+
"events": [
163+
{
164+
"commandStartedEvent": {
165+
"command": {
166+
"update": "coll0",
167+
"updates": [
168+
{
169+
"q": {
170+
"$expr": {
171+
"$eq": [
172+
"$_id",
173+
"$$id"
174+
]
175+
}
176+
},
177+
"u": {
178+
"x": "foo"
179+
}
180+
}
181+
],
182+
"let": {
183+
"id": 1
184+
}
185+
}
186+
}
187+
}
188+
]
189+
}
190+
],
191+
"outcome": [
192+
{
193+
"collectionName": "coll0",
194+
"databaseName": "crud-tests",
195+
"documents": [
196+
{
197+
"_id": 1
198+
},
199+
{
200+
"_id": 2
201+
}
202+
]
203+
}
204+
]
205+
}
206+
]
207+
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
description: "replaceOne-let"
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name crud-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData: &initialData
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1 }
23+
- { _id: 2 }
24+
25+
tests:
26+
- description: "ReplaceOne with let option"
27+
runOnRequirements:
28+
- minServerVersion: "5.0"
29+
operations:
30+
- name: replaceOne
31+
object: *collection0
32+
arguments:
33+
filter: &filter
34+
$expr:
35+
$eq: [ "$_id", "$$id" ]
36+
replacement: &replacement
37+
x: "foo"
38+
let: &let
39+
id: 1
40+
expectResult:
41+
matchedCount: 1
42+
modifiedCount: 1
43+
upsertedCount: 0
44+
expectEvents:
45+
- client: *client0
46+
events:
47+
- commandStartedEvent:
48+
command:
49+
update: *collection0Name
50+
updates:
51+
-
52+
q: *filter
53+
u: *replacement
54+
let: *let
55+
outcome:
56+
-
57+
collectionName: *collection0Name
58+
databaseName: *database0Name
59+
documents:
60+
- { _id: 1, x: "foo" }
61+
- { _id: 2 }
62+
63+
- description: "ReplaceOne with let option unsupported (server-side error)"
64+
runOnRequirements:
65+
- minServerVersion: "3.6.0"
66+
maxServerVersion: "4.4.99"
67+
operations:
68+
- name: replaceOne
69+
object: *collection0
70+
arguments:
71+
filter: *filter
72+
replacement: *replacement
73+
let: *let
74+
expectError:
75+
errorContains: "'update.let' is an unknown field"
76+
isClientError: false
77+
expectEvents:
78+
- client: *client0
79+
events:
80+
- commandStartedEvent:
81+
command:
82+
update: *collection0Name
83+
updates:
84+
-
85+
q: *filter
86+
u: *replacement
87+
let: *let
88+
outcome:
89+
-
90+
collectionName: *collection0Name
91+
databaseName: *database0Name
92+
documents:
93+
- { _id: 1 }
94+
- { _id: 2 }

0 commit comments

Comments
 (0)