Quick start guide for mongodb shell quick start.
Get started with DocumentDB using the MongoDB shell for a familiar MongoDB-compatible experience.
# 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"
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.
# The server will be available at localhost:10260 (or your chosen port)
# You can verify the server is running using:
docker ps
Connection string format
mongosh "mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true"
// Create/switch to a database
use mydb
// Create a collection
db.createCollection("users")
// Create another collection
db.createCollection("logs")
// Insert a single document
db.users.insertOne({ name: "John Doe", email: "john@example.com", created_at: new Date() })
// Insert multiple documents
db.users.insertMany([
{ name: "Jane Smith", email: "jane@example.com" },
{ name: "Bob Johnson", email: "bob@example.com" }
])
// Find all documents
db.users.find()
// Find with criteria
db.users.find({ name: "John Doe" })
// Find with projection
db.users.find({}, { name: 1, email: 1, _id: 0 })
// Complex queries
db.users.find(
{ $and:
[{
created_at: { $gte: new Date("2025-01-01") }
},
{
email: { $regex: "@example.com$" }
}
]
})
// Update a single document
db.users.updateOne({ name: "John Doe" }, { $set: { status: "active" } })
// Update multiple documents
db.users.updateMany({ email: { $regex: "@example.com$" } }, { $set: { domain: "example.com" } })
// Delete a single document
db.users.deleteOne({ name: "John Doe" })
// Delete multiple documents
db.users.deleteMany({ status: "inactive" })