Skip to content

Commit f782001

Browse files
author
Chris Cho
authored
DOCSP-10259: address feedback on update arrays (#97)
* DOCSP-10259: Address feedback on Update Arrays
1 parent f6affeb commit f782001

File tree

3 files changed

+409
-69
lines changed

3 files changed

+409
-69
lines changed

source/code-snippets/crud/arrayFilters.js

Lines changed: 150 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,153 @@ const uri =
77
"mongodb+srv://<user>:<password>@<cluster-url>?w=majority";
88
const client = new MongoClient(uri);
99

10-
async function run() {
10+
11+
async function loadData() {
12+
try {
13+
await client.connect();
14+
15+
const database = client.db("test");
16+
const collection = database.collection("pizza");
17+
18+
await collection.drop();
19+
20+
await collection.insertMany([
21+
{
22+
name: "Steve Lobsters",
23+
address: "731 Yexington Avenue",
24+
items: [
25+
{
26+
type: "pizza",
27+
size: "large",
28+
toppings: ["pepperoni"],
29+
},
30+
{
31+
type: "pizza",
32+
size: "medium",
33+
toppings: ["mushrooms", "sausage", "green peppers"],
34+
comment: "Extra green peppers please!",
35+
},
36+
{
37+
type: "pizza",
38+
size: "large",
39+
toppings: ["pineapple, ham"],
40+
comment: "red pepper flakes on top",
41+
},
42+
{
43+
type: "calzone",
44+
fillings: ["canadian bacon", "sausage", "onion"],
45+
},
46+
{
47+
type: "beverage",
48+
name: "Diet Pepsi",
49+
size: "16oz",
50+
},
51+
],
52+
},
53+
{
54+
name: "Popeye",
55+
address:"1 Sweetwater",
56+
items: [
57+
{
58+
type: "pizza",
59+
size: "large",
60+
toppings: ["garlic", "spinach"]
61+
},
62+
{
63+
type: "calzone",
64+
toppings: ["ham"],
65+
},
66+
]
67+
}
68+
);
69+
70+
console.log(JSON.stringify(await (await collection.find()).toArray()));
71+
} finally {
72+
await client.close();
73+
}
74+
}
75+
76+
77+
async function runAllArrayElements() {
78+
79+
try {
80+
await client.connect();
81+
82+
const database = client.db("test");
83+
const collection = database.collection("pizza");
84+
85+
console.log(JSON.stringify(await (await collection.find()).toArray()));
86+
87+
// start allArrayElement example
88+
const query = { "name": "Popeye" };
89+
const updateDocument = {
90+
$push: { "items.$[].toppings": "fresh mozzarella" }
91+
};
92+
const result = await collection.updateOne(query, updateDocument);
93+
// end allArrayElement example
94+
console.log(result.modifiedCount);
95+
console.log(JSON.stringify(await (await collection.find()).toArray()));
96+
} finally {
97+
await client.close();
98+
}
99+
}
100+
async function runFirstArrayElement() {
101+
102+
try {
103+
await client.connect();
104+
105+
const database = client.db("test");
106+
const collection = database.collection("pizza");
107+
108+
console.log(JSON.stringify(await (await collection.find()).toArray()));
109+
110+
// start firstArrayElement example
111+
const query = { name: "Steve Lobsters", "items.type": "pizza" };
112+
const updateDocument = {
113+
$set: { "items.$.size": "extra large" }
114+
};
115+
const result = await collection.updateOne(query, updateDocument);
116+
// end firstArrayElement example
117+
console.log(result.modifiedCount);
118+
console.log(JSON.stringify(await (await collection.find()).toArray()));
119+
} finally {
120+
await client.close();
121+
}
122+
}
123+
124+
125+
async function arrayFiltersOne() {
126+
try {
127+
await client.connect();
128+
129+
const database = client.db("test");
130+
const collection = database.collection("pizza");
131+
132+
console.log(JSON.stringify(await (await collection.find()).toArray()));
133+
134+
// start arrayFiltersOne example
135+
const query = { name: "Steve Lobsters" };
136+
const updateDocument = {
137+
$push: { "items.$[orderItem].toppings": "garlic" }
138+
};
139+
const options = {
140+
arrayFilters: [{
141+
"orderItem.type": "pizza",
142+
"orderItem.size": "large",
143+
}]
144+
};
145+
146+
const result = await collection.updateMany(query, updateDocument, options);
147+
// end arrayFiltersOne example
148+
149+
console.log(result.modifiedCount);
150+
console.log(JSON.stringify(await (await collection.find()).toArray()));
151+
} finally {
152+
await client.close();
153+
}
154+
}
155+
156+
async function arrayFiltersTwo() {
11157
try {
12158
await client.connect();
13159

@@ -16,7 +162,7 @@ async function run() {
16162

17163
console.log(JSON.stringify(await (await collection.find()).toArray()));
18164

19-
// start arrayFilters example
165+
// start arrayFiltersTwo example
20166
const query = { name: "Steve Lobsters" };
21167
const updateDocument = {
22168
$push: { "items.$[item].toppings": "salami" },
@@ -30,7 +176,7 @@ async function run() {
30176
],
31177
};
32178
const result = await collection.updateOne(query, updateDocument, options);
33-
// end arrayFilters example
179+
// end arrayFiltersTwo example
34180
console.log(result.modifiedCount);
35181

36182
collection.insertOne({
@@ -71,4 +217,4 @@ async function run() {
71217
await client.close();
72218
}
73219
}
74-
run().catch(console.dir);
220+
//run().catch(console.dir);

source/fundamentals/crud/write-operations.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Write Operations
77
.. toctree::
88
:caption: Write Operations
99

10+
/fundamentals/crud/write-operations/change-a-document
1011
/fundamentals/crud/write-operations/embedded-arrays
1112
/fundamentals/crud/write-operations/upsert
12-
/fundamentals/crud/write-operations/change-a-document
13+

0 commit comments

Comments
 (0)