databay.outlets.mongo_outlet

Warning

MongoOutlet requires PyMongo to function. Please install required dependencies using:

pip install "databay[MongoOutlet]"

Contents:

MongoCollectionNotFound

Raised when requested collection does not exist in the database.

ensure_connection

Ensure the MongoDB connection is established before running the function.

MongoOutlet

Outlet for pushing data into a MongoDB instance. Pushes are executed synchronously.

exception databay.outlets.mongo_outlet.MongoCollectionNotFound[source]

databay.outlets.mongo_outlet.MongoCollectionNotFound

Raised when requested collection does not exist in the database.

Initialize self. See help(type(self)) for accurate signature.

Bases: Exception

databay.outlets.mongo_outlet.ensure_connection(fn)[source]

Ensure the MongoDB connection is established before running the function.

Parameters

fn (Callable) – Function to decorate

class databay.outlets.mongo_outlet.MongoOutlet(database_name: str = 'databay', collection: str = 'default_collection', host: str = None, port: str = None, *args, **kwargs)[source]

databay.outlets.mongo_outlet.MongoOutlet

Outlet for pushing data into a MongoDB instance. Pushes are executed synchronously.

Parameters
  • database_name (str) – Name of the MongoDB database to write to.

    Default:
    'databay'

  • collection (str) – Global name of the collection to write to. This can be overwritten by records’ metadata.MONGODB_COLLECTION parameter.

    Default:
    'default_collection'

  • host (str) – Address of MongoDB host.

    Default:
    None (PyMongo defaults to 'localhost')

  • port (int) – Port of the MongoDB host.

    Default:
    None (PyMongo defaults to 27017)

Bases: databay.outlet.Outlet

push(self, records: [Record], update)[source]

ensure_connection

Write records into the database. Writes are executed synchronously.

Parameters
  • records (list[Record]) – List of records generated by inlets. Each top-level element of this array corresponds to one inlet that successfully returned data. Note that inlets could return arrays too, making this a nested array.

  • update (Update) – Update object representing the particular Link transfer.

connect(self, database_name: str = None)bool[source]

Connect to the specified database. Returns True if already connected to the specified database. Disconnects from any existing databases if the specified database is different.

Parameters

database_name (str) – Name of the database to connect to.

Default:
None (Connects to default database name if not specified`)

Returns

Returns True if already connected to the database specified.

Return type

bool

disconnect(self)[source]

Disconnect from the database if currently connected.

on_start(self)[source]

Connect to the MongoDB host on start.

on_shutdown(self)[source]

Disconnect from the MongoDB host on shutdown.