homeresume
 
   
🔍

MongoDB containers with Docker Compose

February 2, 2024

For Compose basics, see the Docker Compose overview. This post covers MongoDB with Mongo Express.

Prerequisites

  • Docker Compose installed

Configuration

The following configuration spins up the MongoDB container with the UI tool (Mongo Express).

The connection string for the MongoDB database is mongodb://localhost:27018.

Mongo Express is available at the http://localhost:8082 link. Use the below Basic auth credentials to log in to Mongo Express.

# docker-compose.yml
services:
mongo:
image: 'mongo:7.0.5'
ports:
- 27018:27017
volumes:
- my-data:/var/lib/mongodb/data
mongo-express:
image: 'mongo-express:1.0.2'
ports:
- 8082:8081
environment:
ME_CONFIG_BASICAUTH_USERNAME: username
ME_CONFIG_BASICAUTH_PASSWORD: password
volumes:
my-data:

Run the following command to spin up the containers.

docker compose up

Demo

Docker Compose files for this post live in the mongodb-docker-compose folder. Get access via code demos.

2021

Cursor-based iteration with Mongoose

November 17, 2021

Using cursor-based iteration can be useful when there is a need to iterate (in memory) over all of the documents from a collection and every document is a big object. Every document can be projected to return only specific fields.

const cursor = Model.find()
.select('/* several fields */')
.cursor();
for (
let document = await cursor.next();
document != null;
document = await cursor.next()
) {
// ...
}

Demo

Runnable Node.js script for this post lives in the cursor-mongoose-demo folder. Get access via code demos.