Mongo Utilities

class mongo.MongoUtils[source]

Houses common database operations (such as cloning a collection) that for some ungodly reason MongoDB does not support natively.

clone_collection(source_collection_name: str, destination_collection_name: str, fields_to_copy: List[str], overwrite_existing: bool, copy_order: str | None = 'ascending')[source]

Clones an existing MongoDB collection item-by-item. This is inefficient, but appears to be the only supported way of doing this.

Parameters:
  • source_collection_name (str) – The name of the source MongoDB collection to clone.

  • destination_collection_name (str) – The desired name of the cloned collection.

  • fields_to_copy (List[str]) – A list of fields to copy from the source collection to the cloned destination collection.

  • overwrite_existing (bool) – A boolean flag indicating if the destination collection should be overwritten during the clone (should it already exist).

  • copy_order (Optional[str]) – A string of the form: 'ascending' or 'descending' indicating the order in which records should be copied from the source collection to the destination collection.

Raises:

OperationFailure – Raises an OperationFailure error if an insert operation fails during the collection cloning process.