Quick start guide for python setup guide.
Learn how to set up and use DocumentDB with Python using the MongoDB Python driver (PyMongo).
# Pull the latest DocumentDB Docker image
docker pull ghcr.io/microsoft/documentdb/documentdb-local:latest
# Tag the image for convenience
docker tag ghcr.io/microsoft/documentdb/documentdb-local:latest documentdb
# Run the container with your chosen username and password
docker run -dt -p 10260:10260 --name documentdb-container documentdb --username <YOUR_USERNAME> --password <YOUR_PASSWORD>
docker image rm -f ghcr.io/microsoft/documentdb/documentdb-local:latest || echo "No existing documentdb image to remove"
During the transition to the Linux Foundation, Docker images may still be hosted on Microsoft's container registry. These will be migrated to the new DocumentDB organization as the transition completes.
Replace <YOUR_USERNAME>
and <YOUR_PASSWORD>
with your desired credentials. You must set these when creating the container for authentication to work.
Port 10260
is used by default in these instructions to avoid conflicts with other local database services. You can use port 27017
(the standard MongoDB port) or any other available port if you prefer. If you do, be sure to update the port number in both your docker run
command and your connection string accordingly.
import pymongo
from pymongo import MongoClient
# Create a MongoDB client and open a connection to DocumentDB
client = pymongo.MongoClient(
'mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true'
)
quickStartDatabase = client["quickStartDatabase"]
quickStartCollection = quickStartDatabase.create_collection("quickStartCollection")
# Insert a single document
quickStartCollection.insert_one({
'name': 'John Doe',
'email': 'john@email.com',
'address': '123 Main St, Anytown, USA',
'phone': '555-1234'
})
# Insert multiple documents
quickStartCollection.insert_many([
{
'name': 'Jane Smith',
'email': 'jane@email.com',
'address': '456 Elm St, Othertown, USA',
'phone': '555-5678'
},
{
'name': 'Alice Johnson',
'email': 'alice@email.com',
'address': '789 Oak St, Sometown, USA',
'phone': '555-8765'
}
])