ahvn.utils.mdb package¶
- ahvn.utils.mdb.resolve_mdb_config(database=None, collection=None, **kwargs)[source]¶
Compile a MongoDB configuration dictionary based on the following order of priority: 1. kwargs 2. global configuration When a parameter is specified in multiple places, the one with the highest priority is used.
- Parameters:
- Returns:
The resolved MongoDB configuration dictionary.
- Return type:
Example
>>> config = resolve_mdb_config(collection="my_collection") >>> config = resolve_mdb_config(host="192.168.1.100") >>> config = resolve_mdb_config(connection_string="mongodb://localhost:27017/mydb")
- class ahvn.utils.mdb.BaseMongoType(**kwargs)[source]¶
Bases:
objectBase class for MongoDB field types with UKF conversion.
- class ahvn.utils.mdb.MongoIdType(**kwargs)[source]¶
Bases:
BaseMongoTypeID type for MongoDB (_id field).
UKF IDs can be very large integers (beyond 64-bit). MongoDB only supports up to 64-bit integers (8 bytes). We store IDs as strings to avoid overflow.
- class ahvn.utils.mdb.MongoTextType(length=None, **kwargs)[source]¶
Bases:
BaseMongoTypeText type for MongoDB (string).
- Parameters:
length (int | None)
- class ahvn.utils.mdb.MongoIntegerType(**kwargs)[source]¶
Bases:
BaseMongoTypeInteger type for MongoDB.
- class ahvn.utils.mdb.MongoBooleanType(**kwargs)[source]¶
Bases:
BaseMongoTypeBoolean type for MongoDB.
- class ahvn.utils.mdb.MongoDurationType(**kwargs)[source]¶
Bases:
BaseMongoTypeDuration type for MongoDB (stored as integer seconds).
- class ahvn.utils.mdb.MongoTimestampType(**kwargs)[source]¶
Bases:
BaseMongoTypeTimestamp type for MongoDB (stored as integer or datetime).
- class ahvn.utils.mdb.MongoJsonType(**kwargs)[source]¶
Bases:
BaseMongoTypeJSON type for MongoDB (stored as embedded document).
- class ahvn.utils.mdb.MongoVectorType(**kwargs)[source]¶
Bases:
BaseMongoTypeVector type for MongoDB (stored as array of floats).
- class ahvn.utils.mdb.MongoTagsType(**kwargs)[source]¶
Bases:
BaseMongoTypeTags type for MongoDB (stored as array of {slot, value} subdocuments).
UKF tags are stored as strings like “[slot:value]”. MongoDB stores them as subdocuments: [{“slot”: “…”, “value”: “…”}, …]
- class ahvn.utils.mdb.MongoSynonymsType(**kwargs)[source]¶
Bases:
BaseMongoTypeSynonyms type for MongoDB (stored as array of strings).
UKF synonyms are already a set of strings.
- class ahvn.utils.mdb.MongoRelatedType(**kwargs)[source]¶
Bases:
BaseMongoTypeRelated type for MongoDB (stored as array of relation subdocuments).
UKF related are 5-element tuples: (subject_id: int, relation: str, object_id: int, relation_id: Optional[int], relation_resources: Optional[str])
The relation_resources is a JSON string that gets stored in MongoDB as-is.
- class ahvn.utils.mdb.MongoAuthsType(**kwargs)[source]¶
Bases:
BaseMongoTypeAuthorities type for MongoDB (stored as array of subdocuments).
UKF stores auths as set of “[user:authority]” strings. We parse this into subdocuments with user and authority fields.
- class ahvn.utils.mdb.MongoCompiler[source]¶
Bases:
objectCompiler that converts KLOp JSON IR to MongoDB MQL.
- class ahvn.utils.mdb.MongoDatabase(database=None, collection=None, connect=False, **kwargs)[source]¶
Bases:
objectMongoDB connection wrapper (PyMongo only, sync operations).
Follows the same pattern as Database (utils/db/base.py): - Uses resolve_mdb_config() to get configuration from HEAVEN_CM - Handles connection string building with defaults - Manages connection lifecycle - Provides generic MongoDB access without UKF-specific logic
Example
```python # Use default config mongo = MongoDatabase() collection = mongo.conn
# Override specific params mongo = MongoDatabase(host=”192.168.1.100”, port=27018)
# Use connection string directly mongo = MongoDatabase(connection_string=”mongodb://localhost:27017/mydb”)
# Context manager (auto-close) with MongoDatabase() as mongo:
collection = mongo.mdb[“test”] collection.insert_one({“name”: “Alice”})
Note
This class uses PyMongo for synchronous operations only. Motor (async) support will be added in a future phase.
- __init__(database=None, collection=None, connect=False, **kwargs)[source]¶
Initialize MongoDB connection.
Similar to Database.__init__(), resolves configuration from HEAVEN_CM.
- Parameters:
database (
Optional[str]) – Database name (optional, defaults to config value)collection (
Optional[str]) – Collection name (optional, defaults to config value)connect (
bool) – Whether to connect immediately upon initialization.**kwargs – Additional connection parameters that override config values - database: Database name (overrides config) - host: MongoDB host (overrides config) - port: MongoDB port (overrides config) - username: Auth username (overrides config) - password: Auth password (overrides config) - connection_string: Full connection string (overrides all) - maxPoolSize, connectTimeoutMS, etc.
- property client: MongoClient¶
Get PyMongo client for sync operations.
- Returns:
The PyMongo client instance.
- Return type:
MongoClient
- property mdb: Database¶
Get the specified database collection.
- Returns:
The MongoDB database instance.
- Return type:
- property conn: Collection¶
Get the specified database collection.
- Returns:
The MongoDB collection instance.
- Return type:
Collection
- create_index(keys, **kwargs)[source]¶
Create index on collection.
- Parameters:
Example
>>> mongo.create_index([("name", 1)], name="users_name_idx") >>> mongo.create_index([("type", 1), ("age", -1)], name="users_type_age_idx", unique=True)
Submodules¶
- ahvn.utils.mdb.base module
- ahvn.utils.mdb.compiler module
- ahvn.utils.mdb.mdb_utils module
- ahvn.utils.mdb.types module