|
1 | 1 | const { MongoClient } = require("mongodb");
|
2 |
| -const stream = require("stream"); |
3 | 2 |
|
4 | 3 | // Replace the following string with your MongoDB deployment's connection string.
|
5 | 4 | const uri =
|
6 | 5 | "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
|
7 | 6 | const client = new MongoClient(uri);
|
8 | 7 |
|
9 |
| - |
10 |
| -async function loadData() { |
| 8 | +async function printData() { |
11 | 9 | try {
|
12 | 10 | await client.connect();
|
| 11 | + const myDB = client.db("test"); |
| 12 | + const myColl = myDB.collection("testColl"); |
13 | 13 |
|
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())); |
70 | 15 | } finally {
|
71 | 16 | await client.close();
|
72 | 17 | }
|
73 | 18 | }
|
74 | 19 |
|
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 |
| -} |
99 | 20 | async function runFirstArrayElement() {
|
100 |
| - |
101 | 21 | try {
|
102 | 22 | await client.connect();
|
| 23 | + const myDB = client.db("test"); |
| 24 | + const myColl = myDB.collection("testColl"); |
103 | 25 |
|
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())); |
108 | 27 |
|
109 | 28 | // start firstArrayElement example
|
110 |
| - const query = { name: "Steve Lobsters", "items.type": "pizza" }; |
| 29 | + const query = { "entries.x": { $type : "string" } }; |
111 | 30 | const updateDocument = {
|
112 |
| - $set: { "items.$.size": "extra large" } |
| 31 | + $inc: { "entries.$.y": 33 } |
113 | 32 | };
|
114 |
| - const result = await pizza.updateOne(query, updateDocument); |
| 33 | + const result = await myColl.updateOne(query, updateDocument); |
115 | 34 | // end firstArrayElement example
|
116 | 35 | console.log(result.modifiedCount);
|
117 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 36 | + console.log(JSON.stringify(await myColl.find().toArray())); |
118 | 37 | } finally {
|
119 | 38 | await client.close();
|
120 | 39 | }
|
121 | 40 | }
|
122 | 41 |
|
123 |
| - |
124 |
| -async function arrayFiltersOne() { |
| 42 | +async function runAllArrayElements() { |
125 | 43 | try {
|
126 | 44 | await client.connect();
|
| 45 | + const myDB = client.db("test"); |
| 46 | + const myColl = myDB.collection("testColl"); |
127 | 47 |
|
128 |
| - const database = client.db("test"); |
129 |
| - const pizza = database.collection("pizza"); |
| 48 | + console.log(JSON.stringify(await myColl.find().toArray())); |
130 | 49 |
|
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" }; |
135 | 52 | const updateDocument = {
|
136 |
| - $push: { "items.$[orderItem].toppings": "garlic" } |
| 53 | + $unset: { "calls.$[].duration": "" } |
137 | 54 | };
|
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 |
148 | 57 | console.log(result.modifiedCount);
|
149 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 58 | + console.log(JSON.stringify(await myColl.find().toArray())); |
150 | 59 | } finally {
|
151 | 60 | await client.close();
|
152 | 61 | }
|
153 | 62 | }
|
154 | 63 |
|
155 |
| -async function arrayFiltersTwo() { |
| 64 | +async function arrayFiltersIdentifier() { |
156 | 65 | try {
|
157 | 66 | await client.connect();
|
| 67 | + const myDB = client.db("test"); |
| 68 | + const myColl = myDB.collection("testColl"); |
158 | 69 |
|
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())); |
163 | 71 |
|
164 |
| - // start arrayFiltersTwo example |
165 |
| - const query = { name: "Steve Lobsters" }; |
| 72 | + // start arrayFiltersIdentifier example |
| 73 | + const query = { date: "11/12/2023" }; |
166 | 74 | const updateDocument = {
|
167 |
| - $push: { "items.$[item].toppings": "salami" }, |
| 75 | + $mul: { "items.$[i].quantity": 2 } |
168 | 76 | };
|
169 | 77 | const options = {
|
170 | 78 | arrayFilters: [
|
171 | 79 | {
|
172 |
| - "item.type": "pizza", |
173 |
| - "item.toppings": "pepperoni", |
174 |
| - }, |
175 |
| - ], |
| 80 | + "i.recipe": "Fried rice", |
| 81 | + "i.item": { $not: { $regex: "oil" } }, |
| 82 | + } |
| 83 | + ] |
176 | 84 | };
|
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 |
179 | 87 | console.log(result.modifiedCount);
|
180 | 88 |
|
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())); |
215 | 90 | } finally {
|
216 | 91 | await client.close();
|
217 | 92 | }
|
|
0 commit comments