@@ -49,47 +49,68 @@ public static void main(String [] args){
49
49
}
50
50
51
51
private void updateValueExample (){
52
+ // Creates a filter and update document to match a document and decrease its first "qty" array value
52
53
// begin updateValueExample
53
54
Bson filter = Filters .eq ("qty" , 18 );
54
55
Bson update = Updates .inc ("qty.$" , -3 );
56
+
57
+ // Defines options that configure the operation to return a document in its post-operation state
55
58
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
56
59
.returnDocument (ReturnDocument .AFTER );
60
+
61
+ // Updates the first document that matches the filter and prints the updated document as JSON
57
62
Document result = collection .findOneAndUpdate (filter , update , options );
58
63
System .out .println (result .toJson ());
59
64
// end updateValueExample
60
65
}
61
66
62
67
private void updateValueOptionsExample (){
68
+ // Creates filter documents to match a document, then match the document's array values under 15
63
69
// begin updateValueOptionsExample
64
70
Bson filter = Filters .eq ("_id" , 1 );
65
71
Bson smallerFilter = Filters .lt ("smaller" , 15 );
72
+
73
+ // Defines options that configure the document's return state and apply the array value filter
66
74
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
67
75
.returnDocument (ReturnDocument .AFTER )
68
76
.arrayFilters (Arrays .asList (smallerFilter ));
77
+
78
+ // Creates an update document to increase the matched array values by "5"
69
79
Bson update = Updates .inc ("qty.$[smaller]" , 5 );
70
80
81
+ // Updates the first document that matches the filter and prints the updated document as JSON
71
82
Document result = collection .findOneAndUpdate (filter , update , options );
72
83
System .out .println (result .toJson ());
73
84
// end updateValueOptionsExample
74
85
}
75
86
76
87
private void updateAllElementsExample (){
88
+ // Creates a filter and update document to match a document and double its "qty" array elements
77
89
// begin updateAllElementsExample
78
90
Bson filter = Filters .eq ("_id" , 1 );
79
91
Bson update = Updates .mul ("qty.$[]" , 2 );
92
+
93
+ // Defines options that configure the operation to return a document in its post-operation state
80
94
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
81
95
.returnDocument (ReturnDocument .AFTER );
96
+
97
+ // Updates the first document that matches the filter and prints the updated document as JSON
82
98
Document result = collection .findOneAndUpdate (filter , update , options );
83
99
System .out .println (result .toJson ());
84
100
// end updateAllElementsExample
85
101
}
86
102
87
103
private void pushElementsExample (){
104
+ // Creates a filter and update document to match a document and add a value to its "qty" array
88
105
// begin pushElementsExample
89
106
Bson filter = Filters .eq ("_id" , 1 );
90
107
Bson update = Updates .push ("qty" , 17 );
108
+
109
+ // Defines options that configure the operation to return a document in its post-operation state
91
110
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions ()
92
111
.returnDocument (ReturnDocument .AFTER );
112
+
113
+ // Updates the first document that matches the filter and prints the updated document as JSON
93
114
Document result = collection .findOneAndUpdate (filter , update , options );
94
115
System .out .println (result .toJson ());
95
116
// end pushElementsExample
0 commit comments