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.

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

database_uri='mongodb://example.com:8100/'
count()[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.