Open-source project
MongoEngine/mongoengine avatar
MongoEngine/mongoengine

MongoEngine's document inheritance and the PyMongo layer it wraps

A Python Object-Document-Mapper for working with MongoDB

4,345 stars1,224 forksPythonMIT

At a glance

What is it?
MongoEngine puts a declarative document layer over PyMongo for Python teams that want field validation and inheritance without hand-writing BSON. The cost is a hard dependency on PyMongo 4.x and validation that lives in Python rather than in the database.
Who is it for?
Adopt MongoEngine when your Python service stores semi-structured documents and you want validation plus subtype queries resolved against a single collection, and when your virtualenv is already free to take PyMongo 4.x. Do not adopt it if you need async access, if you depend on fields or query operators MongoEngine does not wrap, or if you are running a MongoDB version past 8.0 that you cannot afford to test yourself.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 79 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • MongoEngine/mongoengine4,345 ★ · -5
Star history
Repository2026-09-232026-11-24
MongoEngine/mongoengine4.4k4.3k

-5 Stars in the last 62 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

One MongoDB collection behind three Python classes

The decision worth arguing about is not that this library talks to MongoDB, since PyMongo already does that. It is how a single collection gets turned into several Python classes.

The README sets `allow_inheritance` to true on a BlogPost document, then subclasses it as TextPost and LinkPost. Each subclass adds one required field. `BlogPost.objects.count()` returns 2 while the count on each subclass returns 1, so the three classes share storage and a query on the base class still resolves across subtypes. The count split is the reason to accept the abstraction. The price is that saving writes discriminator keys the base class never declared, so a collection written by MongoEngine is not plain readable by an application that talks PyMongo directly unless it tolerates those extra keys.

The repository is arranged around that ownership model. The mongoengine directory holds the package, a benchmarks directory sits beside it, and .readthedocs.yml publishes the user guide from the docs directory.

Field declarations double as write-time validators

`StringField(required=True, max_length=200)` sets the BSON type on write and rejects a document that breaks the constraint before anything reaches the server. Nesting one field inside another, as in `ListField(StringField(max_length=50))`, describes a list of short strings without a separate validator class.

In the README example, the `DateTimeField` default is a timezone-aware call evaluated at write time rather than at import time, so a document created months after the class is defined still gets the correct timestamp and the stored value carries UTC.

Optional dependencies gate features rather than the install. Pillow is needed for ImageField or ImageGridFsProxy, python-dateutil only if you want the more flexible date parser, and blinker only for signals. A minimal install pulls in PyMongo and nothing else, and setup.py states the bound as `pymongo>=4.0,<5.0`.

The PyMongo 4.x upper bound blocks most upgrades

That `>=4.0,<5.0` range is where adoption friction actually sits. A service sharing a virtualenv or a base image with other data libraries may already be pinned to PyMongo 3.x, and moving it is a dependency decision that has nothing to do with mapping.

The example calls `connect('mydb')` with only a database name, which leaves host, port and credentials to PyMongo defaults. Beyond that bare call the README is silent: it points at the tutorial and user guide on Read the Docs rather than showing a connection string, authentication, replica set or TLS setup. If your deployment needs any of those, the README will not answer it.

Async is the other gap. The README documents only the synchronous connect call and names PyMongo as the sole required dependency, so nothing here offers an async mapping path.

Running the suite against a containerised MongoDB 4.4

Installation from PyPI takes one command:

bash
$ python -m pip install -U mongoengine

A source checkout installs with `python -m pip install .` from the repository root. Running the tests is a different matter, because the suite expects a live server on the standard port. The Dockerfile builds from `mongo:4.4.30` and docker-compose.yml maps 27017, so a container is the shortest route to a server matching the oldest version in the tested range.

Install the editable package with its extras and run pytest:

bash
$ python -m pip install -e ".[test]"
$ pytest tests/

To cover every supported Python and PyMongo pair, each interpreter has to be installed first, then tox does the matrix:

bash
# Install tox
$ python -m pip install tox
# Run the test suites
$ tox

Past MongoDB 8.0 you are the test suite

Tested targets are MongoDB v4.4, v5.0, v6.0, v7.0 and v8.0. Newer versions are expected to work, and the project's own instruction for problems there is to open an issue or submit a pull request. Read that as a boundary: above 8.0 you are the one exercising the library.

The last push was on 2026-09-06 and the repository is not archived, so the mapping layer does move.

One naming trap before you search further: `mongoengine.org` is no longer controlled by the project and is described as an expired domain takeover. The maintained sources are the GitHub repository and the Read the Docs site.

Why PyMongo alone can be the better answer

The two libraries are layers rather than rivals. PyMongo hands you dictionaries, the collection API, and direct control over aggregation pipelines and index creation. Nothing is inferred from class definitions and nothing lands in a document you did not put there.

MongoEngine adds validation, defaults, inheritance discriminators and a queryset abstraction, and it takes back the guarantee that your Python object graph mirrors the stored document. When your data is relational in shape, with many joins, transaction-heavy writes and fixed schemas, those constraints are the ones a relational layer was built for. When your access pattern is mostly raw aggregation or hand-tuned operators, the mapper is a layer you maintain for no return.

Setting the two against each other compares a web framework with a relational mapper to a standalone mapping library.

Validation that never reaches the database

Constraints are enforced in Python. A write that bypasses MongoEngine, from a migration script, a shell session or a second service using PyMongo, skips every required flag and length limit and still lands in the same collection. A base-class query then returns documents whose subclass field was never checked, because the rule existed only in the mapping layer.

Editorial conclusion

Adopt MongoEngine when your Python service stores semi-structured documents and you want validation plus subtype queries resolved against a single collection, and when your virtualenv is already free to take PyMongo 4.x. Do not adopt it if you need async access, if you depend on fields or query operators MongoEngine does not wrap, or if you are running a MongoDB version past 8.0 that you cannot afford to test yourself. Before committing a service, write one representative query against a live server using PyMongo 4.x, confirm that the collection you intend to read is not already shaped by another writer, and check that the field types you rely on map onto BSON without a custom encoder.

Frequently asked questions

django vs mongoengine

Django is a web framework whose ORM targets relational databases, while MongoEngine is a standalone object-document mapper for MongoDB with no web layer attached. They address different data shapes rather than competing on the same one.

mongoengine alternative

PyMongo is the layer underneath MongoEngine and serves documents as dictionaries through the collection API. If you want validation, defaults and document inheritance, MongoEngine adds them; if you want raw control over queries and aggregation, PyMongo alone is the alternative.

Does MongoEngine support async Python?

The README documents only the synchronous connect call and lists PyMongo as the single required dependency. The README does not document async usage.

Which MongoDB versions does MongoEngine support?

MongoEngine is tested against MongoDB v4.4, v5.0, v6.0, v7.0 and v8.0. Newer versions are expected to work but are not actively tested.

Official sources

  1. License: MIT
  2. MongoEngine/mongoengine on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/mongoengine-mongoengine.svg)](https://hysenlabs.com/projects/mongoengine-mongoengine)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/mongoengine-mongoengine.svg)](https://hysenlabs.com/projects/mongoengine-mongoengine)

Was this useful?