django-nonreldjango-mongodb-engine

Django non-rel connecting to multiple hosts in a replica set


I'm using django non-rel with mongodb backend. The docs are clear on how to connect to a single host, however I would like to connect to all hosts in a replica set.

Is it possible to do so?

How would the the DATABASES element in settings.py look like for a connection string such as this:

mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test

Solution

  • There is an open issue for this in the mongodb-engine project.

    https://github.com/django-nonrel/mongodb-engine/pull/170#issuecomment-42406797

    From the comments, this is how it should be done:

    DATABASES = {
    
        'default': {
            'ENGINE': 'django_mongodb_engine', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'dbname',                  # Or path to database file if using sqlite3.
            'USER': '',                      # Not used with sqlite3.
            'PASSWORD': '',                  # Not used with sqlite3.
            'HOST': 'mongodb://mongodb1.domain.com:27107, mongodb2.domain.com:27017/?replicaSet=replicaname',
        },
    }
    

    Update:

    This has been verified to work on the main branch:

    git+https://github.com/django-nonrel/mongodb-engine