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)[源代码]¶
-
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)]]¶
- 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)
- 参数:
- 返回:
- {
"_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