MongoDB en Docker

NoSQL MongoDB Docker HomeLab

Veamos como instalar y ejecutar MongDB en un contenedor con Docker.

José R Sosa https://josersosa.github.io/personalweb/
04-26-2025

Fuentes

docker pull mongo
docker run --name some-mongo -d mongo:tag

Connect to MongoDB from another Docker container

The MongoDB server in the image listens on the standard MongoDB port, 27017, so connecting via Docker networks will be the same as connecting to a remote mongod. The following example starts another MongoDB container instance and runs the mongosh (use mongo with 4.x versions) command line client against the original MongoDB container from the example above, allowing you to execute MongoDB statements against your database instance:

docker run -it --network some-network --rm mongo mongosh --host some-mongo test

Example docker-compose.yml for mongo:

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
      ME_CONFIG_BASICAUTH: false

Container shell access and viewing MongoDB logs

The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your mongo container:

docker exec -it some-mongo bash

The MongoDB Server log is available through Docker’s container log:

docker logs some-mongo

Consultas en MongoDB

Documentaciónsobre operadores de consultas en MongoDB Query Operator Array

query <- sprintf('{"Initial_Date": {"$gte": {"$date": "%s"}, "$lte": {"$date": "%s"}}}', start_date, end_date)
query <- '{"Simulation_Name" : "s2"}'
query <- '{"ID" : 3}'
query <- '{"Initial_Date" : {"$gte": {"$date": "2024-09-03T07:00:00Z"}}}'

Si te refieres a hacer el query desde la consola de MongoDB, puedes intentar lo siguiente (estoy usando la versión v3.2) usando el operador de array $all:

db.nombrecoleccion.find({
    'payload.source': {
        $all: ["192.168.100.107", 915]
    }
})

Lo que hace $all es obtener los documentos en donde el valor del campo es un array que contiene los elementos especificados.

Más ejemplos:

    db.nombrecoleccion.find({
        'payload.source.0': "192.168.100.107"
    }) 
    db.nombrecoleccion.find({
        'payload.source.1': {$gt: 900}
    }) 
    db.nombrecoleccion.find({
        'payload.source.0': "192.168.100.107",
        'payload.source.1': {$gt: 900}
    }) 
    db.nombrecoleccion.find({
        'payload.source.0': {
            $in: [
                "192.168.100.105", 
                "192.168.100.107", 
                "192.168.100.109"
             ]
        }
    })

Puedes consultar otros operadores para array en Query Operator Array

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Sosa (2025, April 26). Blog de José R Sosa: MongoDB en Docker. Retrieved from https://josersosa.github.io/personalweb/posts/2026-02-01-mongodb-en-docker/

BibTeX citation

@misc{sosa2025mongodb,
  author = {Sosa, José R},
  title = {Blog de José R Sosa: MongoDB en Docker},
  url = {https://josersosa.github.io/personalweb/posts/2026-02-01-mongodb-en-docker/},
  year = {2025}
}