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)[源代码]

基类: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

示例

```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) ```

参数:
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)[源代码]

Initialize adapter with field selection.

参数:
  • 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)[源代码]
参数:

key (int)

create_indices(mdb)[源代码]
create_vector_index(mdb, dim)[源代码]
from_ukf(kl, key=None, embedding=None)[源代码]

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)

参数:
  • 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)

返回:

{

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

}

返回类型:

Dict with structure

to_ukf_data(document)[源代码]

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

参数:

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

返回类型:

Dict[str, Any]

返回:

Dict suitable for BaseUKF initialization

from_result(result)[源代码]

Convert a query result from MongoDB to the appropriate representation.

参数:

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

返回类型:

Any

返回:

The converted representation (currently returns as-is)