ahvn.utils.mdb.base module

MongoDB connection wrapper for AgentHeaven.

This module provides a MongoDB connection class that follows the same pattern as the Database class but uses PyMongo for MongoDB operations.

class ahvn.utils.mdb.base.MongoDatabase(database=None, collection=None, connect=False, **kwargs)[源代码]

基类:object

MongoDB 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

示例

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

```

备注

This class uses PyMongo for synchronous operations only. Motor (async) support will be added in a future phase.

参数:
  • database (Optional[str])

  • collection (Optional[str])

  • connect (bool)

__init__(database=None, collection=None, connect=False, **kwargs)[源代码]

Initialize MongoDB connection.

Similar to Database.__init__(), resolves configuration from HEAVEN_CM.

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

connect()[源代码]
close()[源代码]

Close MongoDB connection and cleanup resources.

property client: MongoClient

Get PyMongo client for sync operations.

返回:

The PyMongo client instance.

返回类型:

MongoClient

property mdb: Database

Get the specified database collection.

返回:

The MongoDB database instance.

返回类型:

Database

property conn: Collection

Get the specified database collection.

返回:

The MongoDB collection instance.

返回类型:

Collection

create_index(keys, **kwargs)[源代码]

Create index on collection.

参数:
  • keys (List[Tuple[str, int]]) -- List of (field, direction) tuples direction: 1 for ascending, -1 for descending

  • **kwargs -- Additional index options (unique, sparse, etc.)

示例

>>> mongo.create_index([("name", 1)], name="users_name_idx")
>>> mongo.create_index([("type", 1), ("age", -1)], name="users_type_age_idx", unique=True)
create_vector_index(embedding_idx, embedding_field, dim)[源代码]

Create vector search index on collection.

参数:
  • embedding_idx (str) -- Name of the vector index to create.

  • embedding_field (str) -- Field name containing the vector embeddings.

  • dim (int) -- Dimensionality of the embedding vectors.

备注

This only works with MongoDB Atlas, not local MongoDB instances.

__enter__()[源代码]

Context manager entry: returns self.

__exit__(exc_type, exc_val, exc_tb)[源代码]

Context manager exit: closes connection.