MCPcopy
hub / github.com/msiemens/tinydb / update

Method update

tinydb/table.py:370–459  ·  view source on GitHub ↗

Update all matching documents to have a given set of fields. :param fields: the fields that the matching documents will have or a method that will update the documents :param cond: which documents to update :param doc_ids: a list of document I

(
        self,
        fields: Union[Mapping, Callable[[Mapping], None]],
        cond: Optional[QueryLike] = None,
        doc_ids: Optional[Iterable[int]] = None,
    )

Source from the content-addressed store, hash-verified

368 raise RuntimeError('You have to pass either cond or doc_id')
369
370 def update(
371 self,
372 fields: Union[Mapping, Callable[[Mapping], None]],
373 cond: Optional[QueryLike] = None,
374 doc_ids: Optional[Iterable[int]] = None,
375 ) -> List[int]:
376 """
377 Update all matching documents to have a given set of fields.
378
379 :param fields: the fields that the matching documents will have
380 or a method that will update the documents
381 :param cond: which documents to update
382 :param doc_ids: a list of document IDs
383 :returns: a list containing the updated document's ID
384 """
385
386 # Define the function that will perform the update
387 if callable(fields):
388 def perform_update(table, doc_id):
389 # Update documents by calling the update function provided by
390 # the user
391 fields(table[doc_id])
392 else:
393 def perform_update(table, doc_id):
394 # Update documents by setting all fields from the provided data
395 table[doc_id].update(fields)
396
397 if doc_ids is not None:
398 # Perform the update operation for documents specified by a list
399 # of document IDs
400
401 updated_ids = list(doc_ids)
402
403 def updater(table: dict):
404 # Call the processing callback with all document IDs
405 for doc_id in updated_ids:
406 perform_update(table, doc_id)
407
408 # Perform the update operation (see _update_table for details)
409 self._update_table(updater)
410
411 return updated_ids
412
413 elif cond is not None:
414 # Perform the update operation for documents specified by a query
415
416 # Collect affected doc_ids
417 updated_ids = []
418
419 def updater(table: dict):
420 _cond = cast(QueryLike, cond)
421
422 # We need to convert the keys iterator to a list because
423 # we may remove entries from the ``table`` dict during
424 # iteration and doing this without the list conversion would
425 # result in an exception (RuntimeError: dictionary changed size
426 # during iteration)
427 for doc_id in list(table.keys()):

Callers 15

upsertMethod · 0.95
perform_updateMethod · 0.45
test_yamlFunction · 0.45
test_deleteFunction · 0.45
test_add_intFunction · 0.45
test_add_strFunction · 0.45
test_subtractFunction · 0.45
test_setFunction · 0.45
test_incrementFunction · 0.45
test_decrementFunction · 0.45
test_freezeFunction · 0.45

Calls 1

_update_tableMethod · 0.95

Tested by 15

test_yamlFunction · 0.36
test_deleteFunction · 0.36
test_add_intFunction · 0.36
test_add_strFunction · 0.36
test_subtractFunction · 0.36
test_setFunction · 0.36
test_incrementFunction · 0.36
test_decrementFunction · 0.36
test_freezeFunction · 0.36
test_updateFunction · 0.36
test_update_allFunction · 0.36