@@ -113,6 +113,70 @@ After the update operation, the document only has values less than 6:
113
113
114
114
.. _pull-array-of-documents:
115
115
116
+ Remove All Items That Match a Specified ``$pull`` Condition With :method:`~db.collection.bulkWrite()`
117
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
+
119
+ The following :method:`db.collection.bulkWrite()` operation:
120
+
121
+ - Creates the ``profilesBulkWrite`` collection.
122
+
123
+ - Removes all items from the ``votes`` array
124
+ that are greater than or equal to ( :query:`$gte` ) ``6``.
125
+
126
+ - Removes all items from the ``votes`` array
127
+ that are less than or equal to ( :query:`$lte` ) ``3``.
128
+
129
+
130
+ .. code-block:: javascript
131
+
132
+ try {
133
+ db.profilesBulkWrite.bulkWrite( [
134
+ {
135
+ insertOne: {
136
+ "document": { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }
137
+ }
138
+ },
139
+ {
140
+ updateOne: {
141
+ "filter": { _id: 1 },
142
+ "update": { $pull: { votes: { $gte: 6 } } }
143
+ }
144
+ },
145
+ {
146
+ updateOne: {
147
+ "filter": {_id: 1},
148
+ "update": { $pull: { votes: { $lte: 3 } } }
149
+ }
150
+ }
151
+ ] );
152
+ } catch (e) {
153
+ print(e);
154
+ }
155
+
156
+ .. note:: `bulkWrite()`
157
+
158
+ The :method:`db.collection.bulkWrite()` method executes multiple
159
+ write operations listed in an array. In this example, the
160
+ :method:`db.collection.bulkWrite()` performs multiple operations
161
+ on the ``profiles`` collection.
162
+
163
+ After the :method:`db.collection.bulkWrite()` operation,
164
+ you can confirm the document only has values less than
165
+ 6 and greater than 3 using the following
166
+ operation:
167
+
168
+ .. code-block:: javascript
169
+
170
+ db.profilesBulkWrite.find()
171
+
172
+ The operation returns the following:
173
+
174
+ .. code-block:: javascript
175
+ :copyable: false
176
+
177
+ [ { _id: 1, votes: [ 5 ] } ]
178
+
179
+
116
180
Remove Items from an Array of Documents
117
181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
182
0 commit comments