@@ -7,9 +7,12 @@ const client = new MongoClient(uri);
77
88async function printData ( ) {
99 try {
10+
11+ // Get the database and collection on which to run the operation
1012 const myDB = client . db ( "test" ) ;
1113 const myColl = myDB . collection ( "testColl" ) ;
1214
15+ // Print all documents
1316 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
1417 } finally {
1518 await client . close ( ) ;
@@ -18,18 +21,28 @@ async function printData() {
1821
1922async function runFirstArrayElement ( ) {
2023 try {
24+
25+ // Get the database and collection on which to run the operation
2126 const myDB = client . db ( "test" ) ;
2227 const myColl = myDB . collection ( "testColl" ) ;
2328
29+ // Print the result
2430 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
2531
2632 // start firstArrayElement example
33+ // Query for all elements in entries array where the value of x is a string
2734 const query = { "entries.x" : { $type : "string" } } ;
35+
36+ // On first matched element, increase value of y by 33
2837 const updateDocument = {
2938 $inc : { "entries.$.y" : 33 }
3039 } ;
40+
41+ // Execute the update operation
3142 const result = await myColl . updateOne ( query , updateDocument ) ;
3243 // end firstArrayElement example
44+
45+ // Print all documents
3346 console . log ( result . modifiedCount ) ;
3447 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
3548 } finally {
@@ -39,19 +52,30 @@ async function runFirstArrayElement() {
3952
4053async function runAllArrayElements ( ) {
4154 try {
55+
56+ // Get the database and collection on which to run the operation
4257 const myDB = client . db ( "test" ) ;
4358 const myColl = myDB . collection ( "testColl" ) ;
4459
60+ // Print all documents
4561 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
4662
4763 // start allArrayElement example
64+ // Query for all documents where date is the string "5/15/2023"
4865 const query = { date : "5/15/2023" } ;
66+
67+ // For each matched document, remove duration field from all entries in calls array
4968 const updateDocument = {
5069 $unset : { "calls.$[].duration" : "" }
5170 } ;
71+
72+ // Execute the update operation
5273 const result = await myColl . updateOne ( query , updateDocument ) ;
5374 // end allArrayElement example
75+
5476 console . log ( result . modifiedCount ) ;
77+
78+ // Print all documents
5579 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
5680 } finally {
5781 await client . close ( ) ;
@@ -60,16 +84,24 @@ async function runAllArrayElements() {
6084
6185async function arrayFiltersIdentifier ( ) {
6286 try {
87+
88+ // Get the database and collection on which to run the operation
6389 const myDB = client . db ( "test" ) ;
6490 const myColl = myDB . collection ( "testColl" ) ;
6591
92+ // Print all documents
6693 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
6794
6895 // start arrayFiltersIdentifier example
96+ // Query for all documents where date is the string "11/12/2023"
6997 const query = { date : "11/12/2023" } ;
98+
99+ // For each matched document, change the quantity of items to 2
70100 const updateDocument = {
71101 $mul : { "items.$[i].quantity" : 2 }
72102 } ;
103+
104+ // Update only non-oil items used for fried rice
73105 const options = {
74106 arrayFilters : [
75107 {
@@ -78,10 +110,13 @@ async function arrayFiltersIdentifier() {
78110 }
79111 ]
80112 } ;
113+
114+ // Execute the update operation
81115 const result = await myColl . updateOne ( query , updateDocument , options ) ;
82116 // end arrayFiltersIdentifier example
83117 console . log ( result . modifiedCount ) ;
84118
119+ // Print all documents
85120 console . log ( JSON . stringify ( await myColl . find ( ) . toArray ( ) ) ) ;
86121 } finally {
87122 await client . close ( ) ;
0 commit comments