@@ -211,6 +211,64 @@ You can use the ``write()`` method to write objects of the following types to Mo
211
211
212
212
NumPy arrays are specified as ``dict[str, ndarray]``.
213
213
214
+ Ignore Empty Values
215
+ ~~~~~~~~~~~~~~~~~~~
216
+
217
+ The ``write()`` method optionally accepts a boolean ``exclude_none`` parameter.
218
+ If you set this parameter to ``True``, the driver doesn't write empty values to
219
+ the database. If you set this parameter to ``False`` or leave it blank, the
220
+ driver writes ``None`` for each empty field.
221
+
222
+ The code in the following example writes an Arrow ``Table`` to MongoDB twice. One
223
+ of the values in the ``'b'`` field is set to ``None``.
224
+
225
+ The first call to the ``write()`` method omits the ``exclude_none`` parameter,
226
+ so it defaults to ``False``. All values in the ``Table``, including ``None``,
227
+ are written to the database. The second call to the ``write()`` method sets
228
+ ``exclude_none`` to ``True``, so the empty value in the ``'b'`` field is ignored.
229
+
230
+ .. io-code-block::
231
+ :copyable: true
232
+
233
+ .. input::
234
+ :language: python
235
+ :emphasize-lines: 12, 16
236
+
237
+ data_a = [1, 2, 3]
238
+ data_b = [1, None, 3]
239
+
240
+ data = Table.from_pydict(
241
+ {
242
+ "a": data_a,
243
+ "b": data_b,
244
+ },
245
+ )
246
+
247
+ coll.drop()
248
+ write(coll, data)
249
+ col_data = list(coll.find({}))
250
+
251
+ coll.drop()
252
+ write(coll, data, exclude_none=True)
253
+ col_data_exclude_none = list(coll.find({}))
254
+
255
+ print(col_data)
256
+
257
+ print(col_data_exclude_none)
258
+
259
+ .. output::
260
+ :language: json
261
+ :visible: false
262
+ :emphasize-lines: 2, 6
263
+
264
+ {'_id': ObjectId('...'), 'a': 1, 'b': 1}
265
+ {'_id': ObjectId('...'), 'a': 2, 'b': None}
266
+ {'_id': ObjectId('...'), 'a': 3, 'b': 3}
267
+
268
+ {'_id': ObjectId('...'), 'a': 1, 'b': 1}
269
+ {'_id': ObjectId('...'), 'a': 2}
270
+ {'_id': ObjectId('...'), 'a': 3, 'b': 3}
271
+
214
272
Writing to Other Formats
215
273
------------------------
216
274
0 commit comments