MongoDB Storage Adapter

MongoDB Logo

ChatterBot includes support for integration with MongoDB databases via its MongoDatabaseAdapter class.

Before you can use this storage adapter you will need to install pymongo. An easy way to install it is to use the chatterbot[mongodb] extra when installing ChatterBot. For example:

pip install chatterbot[mongodb]

You’ll also need to have a MongoDB server running. An easy way to run one locally is to use Docker:

docker-compose.yml
services:
  mongo:
    # Use the latest stable version of the mongo image
    image: mongo:8.0
    # Expose the default MongoDB port
    ports:
      - "27017:27017"
    # Persist the MongoDB data
    volumes:
      - ./.database/mongodb/db:/data/db

To start the MongoDB container, run:

docker compose up -d

Note

For more information on Docker and docker compose, see the Docker Compose documentation.

Using MongoDB with SSL/TLS

For secure connections to remote MongoDB instances (such as Amazon DocumentDB, MongoDB Atlas, or production deployments), you can use SSL/TLS certificates.

Amazon DocumentDB Example

Amazon DocumentDB requires SSL/TLS connections with a certificate file. Here’s how to configure ChatterBot:

  1. Download the Amazon RDS CA certificate bundle:

wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
  1. Configure ChatterBot to use the certificate:

from chatterbot import ChatBot

bot = ChatBot(
    'MyBot',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    database_uri='mongodb://USERNAME:[email protected]:27017/?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred',
    mongodb_client_kwargs={
        'tlsCAFile': 'global-bundle.pem'  # Path to your certificate file
    }
)

MongoDB Atlas Example

For MongoDB Atlas with SSL/TLS:

from chatterbot import ChatBot

bot = ChatBot(
    'MyBot',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    database_uri='mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority',
    mongodb_client_kwargs={
        'tls': True,
        'tlsAllowInvalidCertificates': False  # Use True only for testing
    }
)

Self-Signed Certificates

If you’re using self-signed certificates:

from chatterbot import ChatBot

bot = ChatBot(
    'MyBot',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    database_uri='mongodb://localhost:27017/chatterbot-database?ssl=true',
    mongodb_client_kwargs={
        'tlsCAFile': '/path/to/ca.pem',
        'tlsCertificateKeyFile': '/path/to/client.pem',
        'tlsAllowInvalidCertificates': False
    }
)

Additional MongoDB Client Options

The mongodb_client_kwargs parameter accepts any valid PyMongo MongoClient options, including:

  • tlsCAFile: Path to CA certificate file

  • tlsCertificateKeyFile: Path to client certificate file

  • tls: Enable/disable TLS

  • tlsAllowInvalidCertificates: Allow invalid certificates (not recommended for production)

  • serverSelectionTimeoutMS: Timeout for server selection

  • connectTimeoutMS: Connection timeout

  • socketTimeoutMS: Socket timeout

  • maxPoolSize: Maximum connection pool size

  • minPoolSize: Minimum connection pool size

For a complete list of options, see the PyMongo MongoClient documentation.

MongoDB Adapter Class Attributes

class chatterbot.storage.MongoDatabaseAdapter(**kwargs)[source]

The MongoDatabaseAdapter is an interface that allows ChatterBot to store statements in a MongoDB database.

Parameters:
  • database_uri (str) – The URI of a remote instance of MongoDB. This can be any valid MongoDB connection string

  • mongodb_client_kwargs (dict) – Additional keyword arguments to pass to the MongoClient constructor. This can include SSL/TLS settings, authentication options, and other PyMongo client configuration parameters.

# Basic connection
database_uri='mongodb://example.com:8100/'

# Connection with SSL/TLS (e.g., Amazon DocumentDB)
database_uri='mongodb://USER:[email protected]:27017/?ssl=true&replicaSet=rs0',
mongodb_client_kwargs={
    'tlsCAFile': 'path/to/rds-combined-ca-bundle.pem'
}
close()[source]

Close the MongoDB client connection.

count() int[source]

Return the number of entries in the database.

create(**kwargs)[source]

Creates a new statement matching the keyword arguments specified. Returns the created statement.

create_many(statements)[source]

Creates multiple statement entries.

drop()[source]

Remove the database.

filter(**kwargs)[source]

Returns a list of statements in the database that match the parameters specified.

get_random()[source]

Returns a random statement from the database

get_statement_model()[source]

Return the class for the statement model.

mongo_to_object(statement_data)[source]

Return Statement object when given data returned from Mongo DB.

remove(statement_text)[source]

Removes the statement that matches the input text.

update(statement)[source]

Modifies an entry in the database. Creates an entry if one does not exist.