Skip to content

Commit 3084cad

Browse files
committed
DOCSP-39983: Add an optional bool flag to the write function to skip writing null fields (#21)
* initial commit * grammar * removing autobuilder workflow * feedback * move note * stringify
1 parent 3a420c8 commit 3084cad

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

.github/workflows/check-autobuilder.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

source/quick-start.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,64 @@ You can use the ``write()`` method to write objects of the following types to Mo
211211

212212
NumPy arrays are specified as ``dict[str, ndarray]``.
213213

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+
214272
Writing to Other Formats
215273
------------------------
216274

0 commit comments

Comments
 (0)