Sling Academy
Home/Python/MongoEngine: How to use multiple databases

MongoEngine: How to use multiple databases

Last updated: February 09, 2024

Introduction

MongoEngine, a Document-Object Mapper (DOM) for working with MongoDB from Python, offers robust support for working with multiple databases. This guide will walk you through the basics to advanced concepts of using multiple databases in MongoEngine. Whether you’re looking to segment your application data across different databases or working with separate databases for security reasons, this tutorial will cover the essentials to get you started.

Getting Started

Before diving into multiple databases, ensure you have MongoEngine installed. If not, install it using pip:

pip install mongoengine

Connect to your MongoDB instance:

from mongoengine import connect
connect('mydb')

This establishes a connection to a MongoDB database named mydb. However, to use multiple databases, you’ll need to define connections distinctly.

Defining Multiple Connections

from mongoengine import connect, register_connection

# Default connection
default_connect = connect('default_db')

# Additional database connection
alternative_db_connect = register_connection(alias='alt_db', name='alternative_db')

The register_connection function allows you to set up additional connections beyond the default. Here, alternative_db is the name of the additional database, while alias is used as a reference throughout your models.

Creating Models for Different Databases

With the connections established, you must define models to utilize these databases. Define a model:

from mongoengine import Document, StringField

class User(Document):
    email = StringField(required=True)

    meta = {'db_alias': 'default_db'}

Here, the User model will use the default_db. To use the alternative_db, specify the alias in the model’s meta:

class Product(Document):
    name = StringField(required=True)

    meta = {'db_alias': 'alt_db'}

Beyond simply assigning models to databases, MongoEngine allows for sophisticated data management strategies.

Advanced Scenarios

Switching databases on the fly, replicating data across databases, and database sharding are more advanced use cases for MongoEngine. Here’s how to dynamically switch databases for a model:

class Order(Document):
    product = StringField(required=True)
    meta = {'db_alias': 'default_db'}

# Dynamically switch database
Order.switch_db('default_db')

This method enables operations on a specific database other than the one originally defined for the model.

Replicating Data Across Databases

To replicate data across databases, a straightforward method is to save the same document to different databases explicitly. This manual approach ensures data redundancy and backup. However, automating this process requires setup outside the scope of MongoEngine and involves more complex MongoDB operations like replica sets.

Conclusion

Utilizing multiple databases in MongoEngine allows for more flexible data management strategies in your applications. From separating concerns to improving security, the capability to manage data across several databases can significantly enhance your projects. By following this guide, you’re well on your way to mastering multiple database management in MongoEngine.

Next Article: MongoEngine: How to define document’s schema

Previous Article: MongoEngine: How to connect to a database

Series: Data Persistence in Python – Tutorials & Examples

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots