Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Use MongoDB as Django session backend

Project description


Forked version from django-mongo-sessions.

Explain why this fork was needed.


  • fast NoSQL Django sessions backend
  • invalidation via TTL


  • Install with pip
pip install django-mongo-sessions
  • Inside
SESSION_ENGINE = 'mongo_sessions.session'

Available settings

There is two ways to setup mongodb connection at First, if already have mongo connection, like::

import pymongo
from pymongo import MongoClient
connection = MongoClient()
MONGO_CLIENT = connection.your_database
MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option

Second, if you need to connect to mongodb, like::

MONGO_PORT = 27017
MONGO_HOST = 'localhost'
MONGO_DB_NAME = 'test'

# all this settings are defaults, you can skip any

expireAfterSeconds index value by default is SESSION_COOKIE_AGE you can change::

MONGO_SESSIONS_TTL = 60 * 60 # one hour

be sure, that you know what are you doing with it, SESSION_COOKIE_AGE will get different expiration time

every time you change one of this values, expireAfterSeconds index will be dropped and then will be indexed with ensureIndex again, be careful here

it is good way to change expireAfterSeconds only by 1 running instance


pip install tox

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django_mdbsessions-0.0.2-py3-none-any.whl (5.1 kB) Copy SHA256 hash SHA256 Wheel py3
django-mdbsessions-0.0.2.tar.gz (3.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page