Installation and Setup
MongoEngine installs as a single package and brings pymongo along as its driver. The rest of setup is having a MongoDB server to talk to - a local instance, a Docker container, or a managed cluster - and a way to verify that Python can actually reach it.
Install
pip install mongoengine
# verify the install and the driver version underneath
python -c "import mongoengine; print(mongoengine.get_version())"
python -c "import pymongo; print(pymongo.version)"
Both versions matter: MongoEngine pins a compatible pymongo range, and mismatched versions are a classic source of confusing connection errors. If a project already depends on pymongo, let the ODM pull the driver version it wants instead of pinning them independently.
Optional Extras
# SRV connection strings (mongodb+srv://) - used by MongoDB Atlas
pip install "pymongo[openssl]" # TLS support (usually already present)
pip install dnspython # SRV record lookup for +srv URIs
The Atlas and most managed clusters use mongodb+srv:// URIs, which require DNS SRV lookup - that is what dnspython provides. For plain mongodb://host:27017 connections nothing extra is needed.
A Local Server
# Docker - the fastest way to get a server for development
docker run -d --name mongo -p 27017:27017 mongo:7
# or start the mongod binary directly if it is installed
mongod --dbpath ~/data/db --port 27017
A dev container with no authentication on localhost:27017 is fine for local work. For anything shared, create a user and use a connection string with credentials (the connecting chapter shows the form). MongoDB Atlas offers a free tier that works the same way from a +srv URI, which is useful for testing the exact production connection path.
A Connection Smoke Test
from mongoengine import connect
connect("smoke_test", host="mongodb://localhost:27017")
print("connected to:", connect.get_last_used())
# ping the server to prove the round trip works
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
print(client.admin.command("ping")) # {'ok': 1.0}
MongoEngine's connect is lazy by default - it does not open the socket until the first query - which is why the explicit ping is the honest smoke test. If the ping returns ok: 1, the stack works: library, driver, server, and network. From here the tutorial assumes a reachable server and focuses on the document model.
Pairing Versions
- Record both versions (mongoengine and pymongo) in your requirements file.
- Match the server major version to the driver's supported range - the driver release notes list it explicitly.
- In CI, run the smoke test against a real (Dockerized) server, not a mock - connection and auth bugs only surface against the real thing.
Summary
Install with pip install mongoengine, add dnspython only if you use mongodb+srv:// URIs, run a MongoDB server via Docker for development, and prove the round trip with an explicit ping - because MongoEngine connects lazily and a silent connection failure will otherwise show up as your first real query error. Pin the driver versions together and the environment is ready for the document model.