Skip to content

Update database items

When calling the method DatabaseManager().add_item() with an object that already exists in the database, meaning the object id is already in use, it will update the already existing database item.

In this example, we promote “Jane Doe” to a professor:

from perseus.datamanager import DatabaseManager, Person
object_id = "asd8820sdjgkklmbnairuqi234"
db_manager = DatabaseManager()
person = db_manager.get_item(Person, search_filter={"firstname": "Jane", "lastname": "Doe"})
if person is not None:
person.title = "Prof."
db_manager.add_item(person)

To update many objects of the same class at once, use DatabaseManager().update_items(). The objects are written in a single bulk operation; each object replaces its existing database document.

from perseus.datamanager import DatabaseManager, Project
db_manager = DatabaseManager()
projects = db_manager.get_items(Project, search_filter={"call": "2026-01"})
if projects:
for project in projects:
project.call = "2026-1"
updated_ids = db_manager.update_items(Project, projects)

Parameters

cls: type[DatabaseItem]

The class of the objects. Every object in items must be an instance of exactly this class, otherwise a TypeError is raised.

items: list[DatabaseItem]

The objects to update. Every object must already exist in the database. If an object has no object id or there is no document with its id, a ValueError is raised and nothing is written.

ordered: bool = True

If True (default), the updates are applied in the given order and the operation stops at the first error. If False, all updates are attempted. See the MongoDB documentation.

Return value

The object ids of the updated objects in the same order as items, or an empty list if items is empty or the database is not reachable.

For every updated object, an update event containing the old and the new version is emitted, see Events.