Skip to content

Commit 067060f

Browse files
committed
DOCSP-29418: improve array updates pg (#673)
* DOCSP-29418: embedded arrays revision * first pass fixes * fixes * small fixes * CC PR fixes 1 * fix * CC PR fixes 2 * CC PR fixes 3 - after discussion * small fixes * build errors * fixes * example revision and other changes * Pr fixes (cherry picked from commit 5c2ef1a) (cherry picked from commit 9e4acb7)
1 parent 2d6e9d8 commit 067060f

File tree

2 files changed

+219
-294
lines changed

2 files changed

+219
-294
lines changed

source/code-snippets/crud/arrayFilters.js

Lines changed: 35 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,217 +1,92 @@
11
const { MongoClient } = require("mongodb");
2-
const stream = require("stream");
32

43
// Replace the following string with your MongoDB deployment's connection string.
54
const uri =
65
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
76
const client = new MongoClient(uri);
87

9-
10-
async function loadData() {
8+
async function printData() {
119
try {
1210
await client.connect();
11+
const myDB = client.db("test");
12+
const myColl = myDB.collection("testColl");
1313

14-
const database = client.db("test");
15-
const pizza = database.collection("pizza");
16-
17-
await pizza.drop();
18-
19-
await pizza.insertMany([
20-
{
21-
name: "Steve Lobsters",
22-
address: "731 Yexington Avenue",
23-
items: [
24-
{
25-
type: "pizza",
26-
size: "large",
27-
toppings: ["pepperoni"],
28-
},
29-
{
30-
type: "pizza",
31-
size: "medium",
32-
toppings: ["mushrooms", "sausage", "green peppers"],
33-
comment: "Extra green peppers please!",
34-
},
35-
{
36-
type: "pizza",
37-
size: "large",
38-
toppings: ["pineapple, ham"],
39-
comment: "red pepper flakes on top",
40-
},
41-
{
42-
type: "calzone",
43-
fillings: ["canadian bacon", "sausage", "onion"],
44-
},
45-
{
46-
type: "beverage",
47-
name: "Diet Pepsi",
48-
size: "16oz",
49-
},
50-
],
51-
},
52-
{
53-
name: "Popeye",
54-
address:"1 Sweetwater",
55-
items: [
56-
{
57-
type: "pizza",
58-
size: "large",
59-
toppings: ["garlic", "spinach"]
60-
},
61-
{
62-
type: "calzone",
63-
toppings: ["ham"],
64-
},
65-
]
66-
}
67-
]);
68-
69-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
14+
console.log(JSON.stringify(await myColl.find().toArray()));
7015
} finally {
7116
await client.close();
7217
}
7318
}
7419

75-
76-
async function runAllArrayElements() {
77-
78-
try {
79-
await client.connect();
80-
81-
const database = client.db("test");
82-
const pizza = database.collection("pizza");
83-
84-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
85-
86-
// start allArrayElement example
87-
const query = { "name": "Popeye" };
88-
const updateDocument = {
89-
$push: { "items.$[].toppings": "fresh mozzarella" }
90-
};
91-
const result = await pizza.updateOne(query, updateDocument);
92-
// end allArrayElement example
93-
console.log(result.modifiedCount);
94-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
95-
} finally {
96-
await client.close();
97-
}
98-
}
9920
async function runFirstArrayElement() {
100-
10121
try {
10222
await client.connect();
23+
const myDB = client.db("test");
24+
const myColl = myDB.collection("testColl");
10325

104-
const database = client.db("test");
105-
const pizza = database.collection("pizza");
106-
107-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
26+
console.log(JSON.stringify(await myColl.find().toArray()));
10827

10928
// start firstArrayElement example
110-
const query = { name: "Steve Lobsters", "items.type": "pizza" };
29+
const query = { "entries.x": { $type : "string" } };
11130
const updateDocument = {
112-
$set: { "items.$.size": "extra large" }
31+
$inc: { "entries.$.y": 33 }
11332
};
114-
const result = await pizza.updateOne(query, updateDocument);
33+
const result = await myColl.updateOne(query, updateDocument);
11534
// end firstArrayElement example
11635
console.log(result.modifiedCount);
117-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
36+
console.log(JSON.stringify(await myColl.find().toArray()));
11837
} finally {
11938
await client.close();
12039
}
12140
}
12241

123-
124-
async function arrayFiltersOne() {
42+
async function runAllArrayElements() {
12543
try {
12644
await client.connect();
45+
const myDB = client.db("test");
46+
const myColl = myDB.collection("testColl");
12747

128-
const database = client.db("test");
129-
const pizza = database.collection("pizza");
48+
console.log(JSON.stringify(await myColl.find().toArray()));
13049

131-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
132-
133-
// start arrayFiltersOne example
134-
const query = { name: "Steve Lobsters" };
50+
// start allArrayElement example
51+
const query = { date: "5/15/2023" };
13552
const updateDocument = {
136-
$push: { "items.$[orderItem].toppings": "garlic" }
53+
$unset: { "calls.$[].duration": "" }
13754
};
138-
const options = {
139-
arrayFilters: [{
140-
"orderItem.type": "pizza",
141-
"orderItem.size": "large",
142-
}]
143-
};
144-
145-
const result = await pizza.updateMany(query, updateDocument, options);
146-
// end arrayFiltersOne example
147-
55+
const result = await myColl.updateOne(query, updateDocument);
56+
// end allArrayElement example
14857
console.log(result.modifiedCount);
149-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
58+
console.log(JSON.stringify(await myColl.find().toArray()));
15059
} finally {
15160
await client.close();
15261
}
15362
}
15463

155-
async function arrayFiltersTwo() {
64+
async function arrayFiltersIdentifier() {
15665
try {
15766
await client.connect();
67+
const myDB = client.db("test");
68+
const myColl = myDB.collection("testColl");
15869

159-
const database = client.db("test");
160-
const pizza = database.collection("pizza");
161-
162-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
70+
console.log(JSON.stringify(await myColl.find().toArray()));
16371

164-
// start arrayFiltersTwo example
165-
const query = { name: "Steve Lobsters" };
72+
// start arrayFiltersIdentifier example
73+
const query = { date: "11/12/2023" };
16674
const updateDocument = {
167-
$push: { "items.$[item].toppings": "salami" },
75+
$mul: { "items.$[i].quantity": 2 }
16876
};
16977
const options = {
17078
arrayFilters: [
17179
{
172-
"item.type": "pizza",
173-
"item.toppings": "pepperoni",
174-
},
175-
],
80+
"i.recipe": "Fried rice",
81+
"i.item": { $not: { $regex: "oil" } },
82+
}
83+
]
17684
};
177-
const result = await pizza.updateOne(query, updateDocument, options);
178-
// end arrayFiltersTwo example
85+
const result = await myColl.updateOne(query, updateDocument, options);
86+
// end arrayFiltersIdentifier example
17987
console.log(result.modifiedCount);
18088

181-
pizza.insertOne({
182-
name: "Steve Lobsters",
183-
address: "731 Yexington Avenue",
184-
items: [
185-
{
186-
type: "pizza",
187-
size: "large",
188-
toppings: ["pepperoni"],
189-
},
190-
{
191-
type: "pizza",
192-
size: "medium",
193-
toppings: ["mushrooms", "sausage", "green peppers"],
194-
comment: "Extra green peppers please!",
195-
},
196-
{
197-
type: "pizza",
198-
size: "large",
199-
toppings: ["pineapple, ham"],
200-
comment: "red pepper flakes on top",
201-
},
202-
{
203-
type: "calzone",
204-
fillings: ["canadian bacon", "sausage", "onion"],
205-
},
206-
{
207-
type: "beverage",
208-
name: "Diet Pepsi",
209-
size: "16oz",
210-
},
211-
],
212-
});
213-
214-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
89+
console.log(JSON.stringify(await myColl.find().toArray()));
21590
} finally {
21691
await client.close();
21792
}

0 commit comments

Comments
 (0)