@@ -49,47 +49,68 @@ public static void main(String [] args){
4949 }
5050
5151 private void updateValueExample (){
52+ // Creates a filter and update document to match a document and decrease its first "qty" array value
5253 // begin updateValueExample
5354 Bson filter = Filters .eq ("qty" , 18 );
5455 Bson update = Updates .inc ("qty.$" , -3 );
56+
57+ // Defines options that configure the operation to return a document in its post-operation state
5558 FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
5659 .returnDocument (ReturnDocument .AFTER );
60+
61+ // Updates the first document that matches the filter and prints the updated document as JSON
5762 Document result = collection .findOneAndUpdate (filter , update , options );
5863 System .out .println (result .toJson ());
5964 // end updateValueExample
6065 }
6166
6267 private void updateValueOptionsExample (){
68+ // Creates filter documents to match a document, then match the document's array values under 15
6369 // begin updateValueOptionsExample
6470 Bson filter = Filters .eq ("_id" , 1 );
6571 Bson smallerFilter = Filters .lt ("smaller" , 15 );
72+
73+ // Defines options that configure the document's return state and apply the array value filter
6674 FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
6775 .returnDocument (ReturnDocument .AFTER )
6876 .arrayFilters (Arrays .asList (smallerFilter ));
77+
78+ // Creates an update document to increase the matched array values by "5"
6979 Bson update = Updates .inc ("qty.$[smaller]" , 5 );
7080
81+ // Updates the first document that matches the filter and prints the updated document as JSON
7182 Document result = collection .findOneAndUpdate (filter , update , options );
7283 System .out .println (result .toJson ());
7384 // end updateValueOptionsExample
7485 }
7586
7687 private void updateAllElementsExample (){
88+ // Creates a filter and update document to match a document and double its "qty" array elements
7789 // begin updateAllElementsExample
7890 Bson filter = Filters .eq ("_id" , 1 );
7991 Bson update = Updates .mul ("qty.$[]" , 2 );
92+
93+ // Defines options that configure the operation to return a document in its post-operation state
8094 FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
8195 .returnDocument (ReturnDocument .AFTER );
96+
97+ // Updates the first document that matches the filter and prints the updated document as JSON
8298 Document result = collection .findOneAndUpdate (filter , update , options );
8399 System .out .println (result .toJson ());
84100 // end updateAllElementsExample
85101 }
86102
87103 private void pushElementsExample (){
104+ // Creates a filter and update document to match a document and add a value to its "qty" array
88105 // begin pushElementsExample
89106 Bson filter = Filters .eq ("_id" , 1 );
90107 Bson update = Updates .push ("qty" , 17 );
108+
109+ // Defines options that configure the operation to return a document in its post-operation state
91110 FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
92111 .returnDocument (ReturnDocument .AFTER );
112+
113+ // Updates the first document that matches the filter and prints the updated document as JSON
93114 Document result = collection .findOneAndUpdate (filter , update , options );
94115 System .out .println (result .toJson ());
95116 // end pushElementsExample
0 commit comments