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 =
@@ -9,212 +8,88 @@ const client = new MongoClient(uri, {
9
8
useUnifiedTopology : true ,
10
9
} ) ;
11
10
12
-
13
- async function loadData ( ) {
11
+ async function printData ( ) {
14
12
try {
15
13
await client . connect ( ) ;
14
+ const myDB = client . db ( "test" ) ;
15
+ const myColl = myDB . collection ( "testColl" ) ;
16
16
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 ( ) ) ) ;
73
18
} finally {
74
19
await client . close ( ) ;
75
20
}
76
21
}
77
22
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
- }
102
23
async function runFirstArrayElement ( ) {
103
-
104
24
try {
105
25
await client . connect ( ) ;
26
+ const myDB = client . db ( "test" ) ;
27
+ const myColl = myDB . collection ( "testColl" ) ;
106
28
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 ( ) ) ) ;
111
30
112
31
// start firstArrayElement example
113
- const query = { name : "Steve Lobsters" , "items. type" : "pizza" } ;
32
+ const query = { "entries.x" : { $ type : "string" } } ;
114
33
const updateDocument = {
115
- $set : { "items .$.size " : "extra large" }
34
+ $inc : { "entries .$.y " : 33 }
116
35
} ;
117
- const result = await pizza . updateOne ( query , updateDocument ) ;
36
+ const result = await myColl . updateOne ( query , updateDocument ) ;
118
37
// end firstArrayElement example
119
38
console . log ( result . modifiedCount ) ;
120
- console . log ( JSON . stringify ( await ( await pizza . find ( ) ) . toArray ( ) ) ) ;
39
+ console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
121
40
} finally {
122
41
await client . close ( ) ;
123
42
}
124
43
}
125
44
126
-
127
- async function arrayFiltersOne ( ) {
45
+ async function runAllArrayElements ( ) {
128
46
try {
129
47
await client . connect ( ) ;
48
+ const myDB = client . db ( "test" ) ;
49
+ const myColl = myDB . collection ( "testColl" ) ;
130
50
131
- const database = client . db ( "test" ) ;
132
- const pizza = database . collection ( "pizza" ) ;
51
+ console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
133
52
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" } ;
138
55
const updateDocument = {
139
- $push : { "items .$[orderItem].toppings " : "garlic " }
56
+ $unset : { "calls .$[].duration " : "" }
140
57
} ;
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
151
60
console . log ( result . modifiedCount ) ;
152
- console . log ( JSON . stringify ( await ( await pizza . find ( ) ) . toArray ( ) ) ) ;
61
+ console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
153
62
} finally {
154
63
await client . close ( ) ;
155
64
}
156
65
}
157
66
158
- async function arrayFiltersTwo ( ) {
67
+ async function arrayFiltersIdentifier ( ) {
159
68
try {
160
69
await client . connect ( ) ;
70
+ const myDB = client . db ( "test" ) ;
71
+ const myColl = myDB . collection ( "testColl" ) ;
161
72
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 ( ) ) ) ;
166
74
167
- // start arrayFiltersTwo example
168
- const query = { name : "Steve Lobsters " } ;
75
+ // start arrayFiltersIdentifier example
76
+ const query = { date : "11/12/2023 " } ;
169
77
const updateDocument = {
170
- $push : { "items.$[item].toppings " : "salami" } ,
78
+ $mul : { "items.$[i].quantity " : 2 }
171
79
} ;
172
80
const options = {
173
81
arrayFilters : [
174
82
{
175
- "item.type " : "pizza " ,
176
- "item.toppings " : "pepperoni" ,
177
- } ,
178
- ] ,
83
+ "i.recipe " : "Fried rice " ,
84
+ "i.item " : { $not : { $regex : "oil" } } ,
85
+ }
86
+ ]
179
87
} ;
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
182
90
console . log ( result . modifiedCount ) ;
183
91
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 ( ) ) ) ;
218
93
} finally {
219
94
await client . close ( ) ;
220
95
}
0 commit comments