Docs/Engines/MongoDB

MongoDB

MongoDB is a document store. There is no SQL DDL: migrations are Python scripts that drive pymongo. A .sql file fails with DBLIFT-NOSQL-001 before anything executes.

Breaking
4.4.0 host-form auth uses the configured database as authSource

The assembled URI was mongodb://user:pass@host:port with no database path; it is now mongodb://user:pass@host:port/<database>. The driver reads that path as the default authSource, so this changes which database credentials are checked against — it does not merely add one. Users defined in the database being migrated now work; users defined in admin (Docker MONGO_INITDB_ROOT_USERNAME, many Atlas setups) are refused. If your user lives in admin, use the url form and name the auth source. Neither shape is affected when authentication is disabled, which is why this is invisible in a default local container.

database:
  type: "mongodb"
  url: "mongodb://user:pass@localhost:27017/?authSource=admin"
  database: "myapp"
type
mongodb
Driver
pymongo
DDL
No transactions
Migrations
Python only

Install

pip install "dblift[mongodb]"

Connection URL

mongodb://user:password@localhost:27017/app

A mongodb:// or mongodb+srv:// URL is accepted as-is. Without a URL, DBLift builds one from host, port, username and password, including /<database> on the path. From 4.4.0 that path is the driver's default authSource — see the breaking note above. The alias mongo also selects this engine. database is required.

dblift.yaml

database:
  type: mongodb
  url: "mongodb://db.internal:27017/app"
  database: "app"

Engine settings

KeyDefaultEffect
url—A mongodb:// or mongodb+srv:// URI. Atlas TLS, replica-set and auth-source options live here.
host / port27017Used when url is omitted. Port defaults to 27017. With username and password, 4.4.0 authenticates against the configured database, not admin.
database—Required. The MongoDB database name. In the host form this is also the default authSource from 4.4.0.

Behaviour notes

SQL migrations are not supported.

Write Python migrations. A .sql file fails with DBLIFT-NOSQL-001. context.execute() of a string raises DBLIFT-NOSQL-002 — MongoDB has no query language for writes.

No transactions, and no transactional DDL.

A migration that fails half-way leaves completed operations in place. Rehearse, and keep each migration small.

schema is not required.

Collections are schemaless. database names the MongoDB database.