Skip to content

Get database items

Every object in the database has a so-called object id, comparable to the primary key in relational databases. You can access this identifier like this:

object_id = myObject.db_id # myObject needs to be an instance of a class which inherits from DatabaseItem
# the object_id above now holds an instance of the class bson.ObjectId, but you usually want to have it as a string:
object_id_str = str(myObject.db_id)

You can use this unique identifier to fetch a single object by using the method DatabaseManager().get_item() and passing the class to fetch as well as the object id:

from perseus.datamanager import DatabaseManager, Project
object_id = "67af092f16affd02f502c903a3"
db_manager = DatabaseManager()
project = db_manager.get_item(Project, oid=object_id)
if project is not None:
...

If there are multiple possible results when calling DatabaseManager().get_item(), PERSEUS will return the first one it fetched from the database. This is not always the first item that has been inserted!

The following example returns a list of all available PERSEUS persons by using the method DatabaseManager().get_items().

from perseus.datamanager import DatabaseManager, Person
db_manager = DatabaseManager()
person_list = db_manager.get_items(Person)
if person_list is not None:
for person in person_list:
...

You have the possibility to filter the results (works on DatabaseManager().get_item(), DatabaseManager().get_items() and DatabaseManager().count_items()). To do so, pass a dictionary to the parameter search_filter, where each key has to be the name of the attribute which you want to search for. Pass your search as value to that key. Only exact matches will be returned.

In this example, we want to fetch all projects which are of type “large”:

from perseus.datamanager import DatabaseManager, Project
project_list = DatabaseManager.get_items(Project, search_filter={"project_type": "large"})
if project_list is not None:
for project in project_list:
...

The search filter is passed to MongoDB as it is, so you can also use MongoDB query operators, e.g. to combine conditions or to search in nested attributes:

from perseus.datamanager import DatabaseManager, Project
db_manager = DatabaseManager()
projects = db_manager.get_items(
Project,
search_filter={
"$and": [
{"project_type": {"$in": ["normal", "large"]}},
{"public_information.approval": True},
]
},
)

DatabaseManager().get_items() accepts the following optional parameters:

sort_by: dict[str, int] | None = None

A MongoDB sort definition: the attribute name as key and 1 (ascending) or -1 (descending) as value.

page: int | None = None and size: int | None = None

Return only one page of the results. page starts at 1. If only size is given, the first size results are returned.

# The ten projects that started most recently
projects = db_manager.get_items(Project, sort_by={"start": -1}, page=1, size=10)

To get the number of objects without loading them, use DatabaseManager().count_items(). It accepts the same search_filter as get_items() and returns the number of matching objects, or None if an error occurred.

number_of_large_projects = db_manager.count_items(Project, search_filter={"project_type": "large"})

DatabaseManager().get_distinct_values() returns every value that occurs for an attribute, each value once. This is useful, for example, to suggest existing values in a form.

calls = db_manager.get_distinct_values(Project, "call", str, allow_none=False)
# e.g. ["2025-02", "2026-01", "2026-03"]

Parameters

  • cls: the class to search in
  • attribute: the name of the attribute; nested attributes are separated by dots, e.g. "source.name"
  • attribute_type: the type of the values (used for type hints only)
  • allow_none: bool = True: if False, None is not part of the result

An empty list is returned if an error occurred.