ahvn.adapter.mdb module¶

MongoDB adapter for converting between UKF objects and MongoDB documents.

class ahvn.adapter.mdb.MongoUKFAdapter(name, include=None, exclude=None, **kwargs)[source]¶

Bases: BaseUKFAdapter

MongoDB adapter for converting between UKF objects and MongoDB documents.

Uses type-based field mapping system (like ORMUKFAdapter and VdbUKFAdapter): - Dynamically maps UKF fields based on BaseUKF.schema() - Uses MONGO_FIELD_TYPES for type conversion - Handles virtual fields (_key, _vec) - Supports multi-valued fields as embedded arrays

Example

```python adapter = MongoUKFAdapter(name=”test_adapter”)

# Convert UKF to MongoDB document ukf = BaseUKF(id=1, name=”test”, type=”test_type”) doc = adapter.from_ukf(ukf) # doc = {“_id”: 1, “name”: “test”, “type”: “test_type”, …}

# Convert MongoDB document back to UKF data = adapter.to_ukf_data(doc) ukf_restored = BaseUKF.from_dict(data, polymorphic=True) ```

Parameters:
virtual_fields = ('id', 'expiration_timestamp')¶
indices = [[('type', 1), ('name', 1), ('version', 1), ('variant', 1)], [('type', 1), ('workspace', 1), ('collection', 1)], [('type', 1), ('timestamp', -1)], [('creator', 1), ('owner', 1)], [('expiration_timestamp', 1)], [('tags.slot', 1), ('tags.value', 1)], [('auths.user_id', 1), ('auths.authority', 1)], [('synonyms', 1)], [('related.relation', 1)]]¶
__init__(name, include=None, exclude=None, **kwargs)[source]¶

Initialize adapter with field selection.

Parameters:
  • name (str) – Adapter name

  • include (Optional[List[str]]) – Fields to include (if None, includes all BaseUKF fields)

  • exclude (Optional[List[str]]) – Fields to exclude

  • **kwargs – Additional parameters (reserved for future use)

parse_id(key)[source]¶
Parameters:

key (int)

create_indices(mdb)[source]¶
create_vector_index(mdb, dim)[source]¶
from_ukf(kl, key=None, embedding=None)[source]¶

Convert BaseUKF to MongoDB document using type-based mapping.

Iterates through self.fields and uses MONGO_FIELD_TYPES for conversion: - For each field in self.fields:

  • Get field type from BaseUKF.schema()

  • Get corresponding MongoFieldType from MONGO_FIELD_TYPES

  • Call field_type.from_ukf(ukf_value) to convert

  • Handle virtual fields separately (_key, _vec)

Parameters:
  • kl (BaseUKF) – BaseUKF object to convert

  • key (Optional[str]) – Optional key for vector search (stored as _key)

  • embedding (Optional[List[float]]) – Optional embedding vector (stored as _vec)

Returns:

{

“_id”: <int>, “name”: “…”, “type”: “…”, “tags”: [{“slot”: “…”, “value”: “…”}, …], “auths”: [{“user_id”: “…”, “authority”: “…”}, …], “synonyms”: [”…”, “…”], “related”: [{…}, …], “_key”: “…”, “_vec”: […], …other fields based on include/exclude…

}

Return type:

Dict with structure

to_ukf_data(document)[source]¶

Convert MongoDB document to UKF initialization dict using type-based mapping.

Iterates through self.fields and uses MONGO_FIELD_TYPES for conversion: - For each field in self.fields:

  • Get field type from BaseUKF.schema()

  • Get corresponding MongoFieldType from MONGO_FIELD_TYPES

  • Call field_type.to_ukf(mongo_value) to convert

Parameters:

document (Dict[str, Any]) – MongoDB document to convert

Return type:

Dict[str, Any]

Returns:

Dict suitable for BaseUKF initialization

from_result(result)[source]¶

Convert a query result from MongoDB to the appropriate representation.

Parameters:

result (Any) – The raw result from a MongoDB query

Return type:

Any

Returns:

The converted representation (currently returns as-is)