|
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 |
| - 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"); |
66 | 12 |
|
67 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 13 | + console.log(JSON.stringify(await myColl.find().toArray())); |
68 | 14 | } finally {
|
69 | 15 | await client.close();
|
70 | 16 | }
|
71 | 17 | }
|
72 | 18 |
|
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 |
| -} |
95 | 19 | async function runFirstArrayElement() {
|
96 |
| - |
97 | 20 | 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"); |
100 | 23 |
|
101 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 24 | + console.log(JSON.stringify(await myColl.find().toArray())); |
102 | 25 |
|
103 | 26 | // start firstArrayElement example
|
104 |
| - const query = { name: "Steve Lobsters", "items.type": "pizza" }; |
| 27 | + const query = { "entries.x": { $type : "string" } }; |
105 | 28 | const updateDocument = {
|
106 |
| - $set: { "items.$.size": "extra large" } |
| 29 | + $inc: { "entries.$.y": 33 } |
107 | 30 | };
|
108 |
| - const result = await pizza.updateOne(query, updateDocument); |
| 31 | + const result = await myColl.updateOne(query, updateDocument); |
109 | 32 | // end firstArrayElement example
|
110 | 33 | console.log(result.modifiedCount);
|
111 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 34 | + console.log(JSON.stringify(await myColl.find().toArray())); |
112 | 35 | } finally {
|
113 | 36 | await client.close();
|
114 | 37 | }
|
115 | 38 | }
|
116 | 39 |
|
117 |
| - |
118 |
| -async function arrayFiltersOne() { |
| 40 | +async function runAllArrayElements() { |
119 | 41 | 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"); |
122 | 44 |
|
123 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 45 | + console.log(JSON.stringify(await myColl.find().toArray())); |
124 | 46 |
|
125 |
| - // start arrayFiltersOne example |
126 |
| - const query = { name: "Steve Lobsters" }; |
| 47 | + // start allArrayElement example |
| 48 | + const query = { date: "5/15/2023" }; |
127 | 49 | 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": "" } |
135 | 51 | };
|
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 |
140 | 54 | console.log(result.modifiedCount);
|
141 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 55 | + console.log(JSON.stringify(await myColl.find().toArray())); |
142 | 56 | } finally {
|
143 | 57 | await client.close();
|
144 | 58 | }
|
145 | 59 | }
|
146 | 60 |
|
147 |
| -async function arrayFiltersTwo() { |
| 61 | +async function arrayFiltersIdentifier() { |
148 | 62 | 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"); |
151 | 65 |
|
152 |
| - console.log(JSON.stringify(await (await pizza.find()).toArray())); |
| 66 | + console.log(JSON.stringify(await myColl.find().toArray())); |
153 | 67 |
|
154 |
| - // start arrayFiltersTwo example |
155 |
| - const query = { name: "Steve Lobsters" }; |
| 68 | + // start arrayFiltersIdentifier example |
| 69 | + const query = { date: "11/12/2023" }; |
156 | 70 | const updateDocument = {
|
157 |
| - $push: { "items.$[item].toppings": "salami" }, |
| 71 | + $mul: { "items.$[i].quantity": 2 } |
158 | 72 | };
|
159 | 73 | const options = {
|
160 | 74 | arrayFilters: [
|
161 | 75 | {
|
162 |
| - "item.type": "pizza", |
163 |
| - "item.toppings": "pepperoni", |
164 |
| - }, |
165 |
| - ], |
| 76 | + "i.recipe": "Fried rice", |
| 77 | + "i.item": { $not: { $regex: "oil" } }, |
| 78 | + } |
| 79 | + ] |
166 | 80 | };
|
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 |
169 | 83 | console.log(result.modifiedCount);
|
170 | 84 |
|
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())); |
205 | 86 | } finally {
|
206 | 87 | await client.close();
|
207 | 88 | }
|
|
0 commit comments