Skip to content

Commit 3220c06

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)
1 parent 157f483 commit 3220c06

File tree

2 files changed

+219
-288
lines changed

2 files changed

+219
-288
lines changed

source/code-snippets/crud/arrayFilters.js

Lines changed: 35 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,207 +1,88 @@
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 {
12-
const database = client.db("test");
13-
const pizza = database.collection("pizza");
14-
15-
await pizza.drop();
16-
17-
await pizza.insertMany([
18-
{
19-
name: "Steve Lobsters",
20-
address: "731 Yexington Avenue",
21-
items: [
22-
{
23-
type: "pizza",
24-
size: "large",
25-
toppings: ["pepperoni"],
26-
},
27-
{
28-
type: "pizza",
29-
size: "medium",
30-
toppings: ["mushrooms", "sausage", "green peppers"],
31-
comment: "Extra green peppers please!",
32-
},
33-
{
34-
type: "pizza",
35-
size: "large",
36-
toppings: ["pineapple, ham"],
37-
comment: "red pepper flakes on top",
38-
},
39-
{
40-
type: "calzone",
41-
fillings: ["canadian bacon", "sausage", "onion"],
42-
},
43-
{
44-
type: "beverage",
45-
name: "Diet Pepsi",
46-
size: "16oz",
47-
},
48-
],
49-
},
50-
{
51-
name: "Popeye",
52-
address:"1 Sweetwater",
53-
items: [
54-
{
55-
type: "pizza",
56-
size: "large",
57-
toppings: ["garlic", "spinach"]
58-
},
59-
{
60-
type: "calzone",
61-
toppings: ["ham"],
62-
},
63-
]
64-
}
65-
]);
10+
const myDB = client.db("test");
11+
const myColl = myDB.collection("testColl");
6612

67-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
13+
console.log(JSON.stringify(await myColl.find().toArray()));
6814
} finally {
6915
await client.close();
7016
}
7117
}
7218

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

101-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
24+
console.log(JSON.stringify(await myColl.find().toArray()));
10225

10326
// start firstArrayElement example
104-
const query = { name: "Steve Lobsters", "items.type": "pizza" };
27+
const query = { "entries.x": { $type : "string" } };
10528
const updateDocument = {
106-
$set: { "items.$.size": "extra large" }
29+
$inc: { "entries.$.y": 33 }
10730
};
108-
const result = await pizza.updateOne(query, updateDocument);
31+
const result = await myColl.updateOne(query, updateDocument);
10932
// end firstArrayElement example
11033
console.log(result.modifiedCount);
111-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
34+
console.log(JSON.stringify(await myColl.find().toArray()));
11235
} finally {
11336
await client.close();
11437
}
11538
}
11639

117-
118-
async function arrayFiltersOne() {
40+
async function runAllArrayElements() {
11941
try {
120-
const database = client.db("test");
121-
const pizza = database.collection("pizza");
42+
const myDB = client.db("test");
43+
const myColl = myDB.collection("testColl");
12244

123-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
45+
console.log(JSON.stringify(await myColl.find().toArray()));
12446

125-
// start arrayFiltersOne example
126-
const query = { name: "Steve Lobsters" };
47+
// start allArrayElement example
48+
const query = { date: "5/15/2023" };
12749
const updateDocument = {
128-
$push: { "items.$[orderItem].toppings": "garlic" }
129-
};
130-
const options = {
131-
arrayFilters: [{
132-
"orderItem.type": "pizza",
133-
"orderItem.size": "large",
134-
}]
50+
$unset: { "calls.$[].duration": "" }
13551
};
136-
137-
const result = await pizza.updateMany(query, updateDocument, options);
138-
// end arrayFiltersOne example
139-
52+
const result = await myColl.updateOne(query, updateDocument);
53+
// end allArrayElement example
14054
console.log(result.modifiedCount);
141-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
55+
console.log(JSON.stringify(await myColl.find().toArray()));
14256
} finally {
14357
await client.close();
14458
}
14559
}
14660

147-
async function arrayFiltersTwo() {
61+
async function arrayFiltersIdentifier() {
14862
try {
149-
const database = client.db("test");
150-
const pizza = database.collection("pizza");
63+
const myDB = client.db("test");
64+
const myColl = myDB.collection("testColl");
15165

152-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
66+
console.log(JSON.stringify(await myColl.find().toArray()));
15367

154-
// start arrayFiltersTwo example
155-
const query = { name: "Steve Lobsters" };
68+
// start arrayFiltersIdentifier example
69+
const query = { date: "11/12/2023" };
15670
const updateDocument = {
157-
$push: { "items.$[item].toppings": "salami" },
71+
$mul: { "items.$[i].quantity": 2 }
15872
};
15973
const options = {
16074
arrayFilters: [
16175
{
162-
"item.type": "pizza",
163-
"item.toppings": "pepperoni",
164-
},
165-
],
76+
"i.recipe": "Fried rice",
77+
"i.item": { $not: { $regex: "oil" } },
78+
}
79+
]
16680
};
167-
const result = await pizza.updateOne(query, updateDocument, options);
168-
// end arrayFiltersTwo example
81+
const result = await myColl.updateOne(query, updateDocument, options);
82+
// end arrayFiltersIdentifier example
16983
console.log(result.modifiedCount);
17084

171-
pizza.insertOne({
172-
name: "Steve Lobsters",
173-
address: "731 Yexington Avenue",
174-
items: [
175-
{
176-
type: "pizza",
177-
size: "large",
178-
toppings: ["pepperoni"],
179-
},
180-
{
181-
type: "pizza",
182-
size: "medium",
183-
toppings: ["mushrooms", "sausage", "green peppers"],
184-
comment: "Extra green peppers please!",
185-
},
186-
{
187-
type: "pizza",
188-
size: "large",
189-
toppings: ["pineapple, ham"],
190-
comment: "red pepper flakes on top",
191-
},
192-
{
193-
type: "calzone",
194-
fillings: ["canadian bacon", "sausage", "onion"],
195-
},
196-
{
197-
type: "beverage",
198-
name: "Diet Pepsi",
199-
size: "16oz",
200-
},
201-
],
202-
});
203-
204-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
85+
console.log(JSON.stringify(await myColl.find().toArray()));
20586
} finally {
20687
await client.close();
20788
}

0 commit comments

Comments
 (0)