Getting Started
Use PyMongo to connect to DocumentDB, verify authentication and TLS, and run your first document queries from Python.
If yes, keep it and continue with the client prerequisites below. Otherwise, choose one server installation:
The Docker Quick Start and Linux Packages Quick Start include the full server instructions. Do not start a second instance if one is already running.
These examples connect to localhost:10260, so run the client on the same host as DocumentDB. Use the credentials chosen for Docker, or username admin and the password chosen during Linux package setup. If you changed the endpoint, use its configured host and port.
Self-signed certificate bypasses below are for local development only. For network access, use a trusted certificate. Linux package setup binds the gateway on all interfaces by default: firewall port 10260 before setup and follow network and certificate guidance. Docker examples publish only on loopback.
Set these in the terminal that will run your application. Replace the placeholders with your existing instance's credentials (for Linux packages, admin and your setup password). The driver passes them as raw values, not embedded in a connection URI.
export DOCUMENTDB_USERNAME='<YOUR_USERNAME>'
export DOCUMENTDB_PASSWORD='<YOUR_PASSWORD>'Skip this if you installed Linux packages or already have a running instance. If you chose Docker and have Docker installed, replace the placeholders below with your chosen credentials. This self-contained command also sets the environment variables read by your application:
export DOCUMENTDB_USERNAME='<YOUR_USERNAME>'
export DOCUMENTDB_PASSWORD='<YOUR_PASSWORD>'
docker run -dt --name documentdb \
-p 127.0.0.1:10260:10260 \
ghcr.io/documentdb/documentdb/documentdb-local:latest \
--username "${DOCUMENTDB_USERNAME:?Set DOCUMENTDB_USERNAME}" \
--password "${DOCUMENTDB_PASSWORD:?Set DOCUMENTDB_PASSWORD}"Wait for the readiness banner in docker logs documentdb before connecting; see Docker Quick Start.
python3 -m venv .venv
source .venv/bin/activateIf you do not use a virtual environment, run the next commands with the Python interpreter you plan to use for your app.
python -m pip install pymongoPyMongo already includes the bson package it needs. Do not install the separate bson package from PyPI.
Create a quickstart.py file. The certificate bypass is for local development only, with the default self-signed certificate from Linux package setup or Docker.
import os
from pymongo import MongoClient
username = os.environ.get("DOCUMENTDB_USERNAME")
password = os.environ.get("DOCUMENTDB_PASSWORD")
if not username or not password:
raise RuntimeError(
"Set DOCUMENTDB_USERNAME and DOCUMENTDB_PASSWORD before running this script"
)
client = MongoClient(
"mongodb://localhost:10260/",
username=username,
password=password,
authSource="admin",
tls=True,
tlsAllowInvalidCertificates=True,
)
try:
client.admin.command("ping")
db = client["quickstart"]
movies = db["movies"]
movies.delete_many({})
movies.insert_many(
[
{"title": "The Matrix", "year": 1999, "genres": ["sci-fi", "action"]},
{"title": "Dune", "year": 2021, "genres": ["sci-fi", "adventure"]},
{"title": "Arrival", "year": 2016, "genres": ["sci-fi", "drama"]},
]
)
movies.create_index("title")
for movie in movies.find(
{"year": {"$gte": 2000}},
{"_id": 0, "title": 1, "year": 1},
).sort("year", -1):
print(movie)
finally:
client.close()Run the script:
python quickstart.pyYou should see the recent movie documents printed after a successful ping.
Sample data is opt-in, not required for your first insert and read. Linux package installations can add --load-sample-data during setup, which needs one extra tool; see Set up and connect. Docker installations can start with --init-data true. Without these options, StoreData does not exist. Docker seeds the sample once per data volume: re-creating the container with --init-data true on a volume that was never seeded loads it without touching your data, and seeding again needs a new volume.
If you loaded the sample, add this snippet after client.admin.command("ping"):
for store in client["StoreData"]["stores"].find(
{},
{"_id": 0, "name": 1, "city": 1, "sales.revenue": 1},
).limit(3):
print(store)If you want certificate validation instead of tlsAllowInvalidCertificates=true, obtain the trusted certificate or CA file for your endpoint and replace the original MongoClient call with the version below. For Linux packages, follow certificate configuration. For Docker, copy the local certificate with:
docker cp documentdb:/home/documentdb/.local/state/documentdb-gateway/tls/cert.pem ~/documentdb-cert.pemclient = MongoClient(
"mongodb://localhost:10260/",
username=username,
password=password,
authSource="admin",
tls=True,
tlsCAFile="/absolute/path/documentdb-cert.pem",
)If the Python quick start does not work on the first try:
docker ps --filter "name=documentdb" and docker logs documentdbsudo documentdb-setup --status; for a manually built gateway, confirm its process is listening on port 10260pymongo, verify the active interpreter with python -c "import sys; print(sys.executable)" and reinstall with python -m pip install pymongotlsAllowInvalidCertificates=true or switch to a trusted local certificate with tlsCAFile