Skip to content

Commit d5af5b6

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 5a50abe commit d5af5b6

File tree

2 files changed

+214
-289
lines changed

2 files changed

+214
-289
lines changed

source/code-snippets/crud/arrayFilters.js

Lines changed: 35 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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 =
@@ -9,212 +8,88 @@ const client = new MongoClient(uri, {
98
useUnifiedTopology: true,
109
});
1110

12-
13-
async function loadData() {
11+
async function printData() {
1412
try {
1513
await client.connect();
14+
const myDB = client.db("test");
15+
const myColl = myDB.collection("testColl");
1616

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

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

107-
const database = client.db("test");
108-
const pizza = database.collection("pizza");
109-
110-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
29+
console.log(JSON.stringify(await myColl.find().toArray()));
11130

11231
// start firstArrayElement example
113-
const query = { name: "Steve Lobsters", "items.type": "pizza" };
32+
const query = { "entries.x": { $type : "string" } };
11433
const updateDocument = {
115-
$set: { "items.$.size": "extra large" }
34+
$inc: { "entries.$.y": 33 }
11635
};
117-
const result = await pizza.updateOne(query, updateDocument);
36+
const result = await myColl.updateOne(query, updateDocument);
11837
// end firstArrayElement example
11938
console.log(result.modifiedCount);
120-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
39+
console.log(JSON.stringify(await myColl.find().toArray()));
12140
} finally {
12241
await client.close();
12342
}
12443
}
12544

126-
127-
async function arrayFiltersOne() {
45+
async function runAllArrayElements() {
12846
try {
12947
await client.connect();
48+
const myDB = client.db("test");
49+
const myColl = myDB.collection("testColl");
13050

131-
const database = client.db("test");
132-
const pizza = database.collection("pizza");
51+
console.log(JSON.stringify(await myColl.find().toArray()));
13352

134-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
135-
136-
// start arrayFiltersOne example
137-
const query = { name: "Steve Lobsters" };
53+
// start allArrayElement example
54+
const query = { date: "5/15/2023" };
13855
const updateDocument = {
139-
$push: { "items.$[orderItem].toppings": "garlic" }
56+
$unset: { "calls.$[].duration": "" }
14057
};
141-
const options = {
142-
arrayFilters: [{
143-
"orderItem.type": "pizza",
144-
"orderItem.size": "large",
145-
}]
146-
};
147-
148-
const result = await pizza.updateMany(query, updateDocument, options);
149-
// end arrayFiltersOne example
150-
58+
const result = await myColl.updateOne(query, updateDocument);
59+
// end allArrayElement example
15160
console.log(result.modifiedCount);
152-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
61+
console.log(JSON.stringify(await myColl.find().toArray()));
15362
} finally {
15463
await client.close();
15564
}
15665
}
15766

158-
async function arrayFiltersTwo() {
67+
async function arrayFiltersIdentifier() {
15968
try {
16069
await client.connect();
70+
const myDB = client.db("test");
71+
const myColl = myDB.collection("testColl");
16172

162-
const database = client.db("test");
163-
const pizza = database.collection("pizza");
164-
165-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
73+
console.log(JSON.stringify(await myColl.find().toArray()));
16674

167-
// start arrayFiltersTwo example
168-
const query = { name: "Steve Lobsters" };
75+
// start arrayFiltersIdentifier example
76+
const query = { date: "11/12/2023" };
16977
const updateDocument = {
170-
$push: { "items.$[item].toppings": "salami" },
78+
$mul: { "items.$[i].quantity": 2 }
17179
};
17280
const options = {
17381
arrayFilters: [
17482
{
175-
"item.type": "pizza",
176-
"item.toppings": "pepperoni",
177-
},
178-
],
83+
"i.recipe": "Fried rice",
84+
"i.item": { $not: { $regex: "oil" } },
85+
}
86+
]
17987
};
180-
const result = await pizza.updateOne(query, updateDocument, options);
181-
// end arrayFiltersTwo example
88+
const result = await myColl.updateOne(query, updateDocument, options);
89+
// end arrayFiltersIdentifier example
18290
console.log(result.modifiedCount);
18391

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

0 commit comments

Comments
 (0)