Skip to content

MongoDB schema & indexes for messages and conversations#231

Open
butuz0 wants to merge 7 commits into
developfrom
mongo-db-schema
Open

MongoDB schema & indexes for messages and conversations#231
butuz0 wants to merge 7 commits into
developfrom
mongo-db-schema

Conversation

@butuz0

@butuz0 butuz0 commented Nov 16, 2025

Copy link
Copy Markdown
Collaborator
  1. Configured MongoDB and Mongo-Express services.
  2. Added mongodb to settings.py DATABASES, configured DATABASE_ROUTERS to avoid relations and migrations conflicts when using two databases. Configuring project to use MongoDB.
  3. Created conversations with MongoDB documents Conversation and Message with default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField". Creating MongoDB documents.
  4. Created indexes for Conversation and Message documents. Collections and Indexes are created automatically by running mongodb migrations. Adding MongoDB Indexes.
  5. Configured pytest to use settings_test.py. Added cleanup fixture for MongoDB tests.

Comment thread docker-compose.yml
Comment on lines +90 to +111
mongodb:
image: mongodb/mongodb-community-server:8.0-ubi8
container_name: mongodb
ports:
- "27017:27017"
env_file:
- ./backend/.env
volumes:
- mongodb-data:/data/db

mongo-express:
image: mongo-express:1-20-alpine3.19
container_name: mongo-express
ports:
- "8081:8081"
env_file:
- ./backend/.env
environment:
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_PORT=27017
depends_on:
- mongodb

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR exposes MongoDB (port 27017) and mongo-express (port 8081) on the host with no clear authentication or network restrictions. This can leak data or allow remote modification of the development database when docker-compose is started on any host.
Restrict exposure in docker-compose for non-local development (remove host ports or bind to localhost), enable authentication, and document the risk. Example change:

mongodb:
  # remove "27017:27017" or change to "127.0.0.1:27017:27017" for local-only bind
  ports:
    - "127.0.0.1:27017:27017"

mongo-express:
  ports:
    - "127.0.0.1:8081:8081"
  environment:
    - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}
    - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_INITDB_ROOT_PASSWORD}

Also add docs warning and ensure credentials are set via .env in CI/CD.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authentication is enabled by setting mongodb and mongo-exopress credentials in backend/.env, which containers load through env_file.
Originally i avoided setting those values directly in docker-compose in the way you suggested: ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_INITDB_ROOT_USERNAME}, etc, because in that case I would also need a top-level .env file next to the docker-compose file to supply those variables.
I have changed ports bindings and added docs backend/docs/services/mongodb.md.
I apologize if i have misunderstood you somewhere.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixture is declared autouse=True, scope="session", so the MongoDB test DB is dropped only before and after the whole test session. Tests in tests/test_mongodb.py assume an empty DB per test (they assert count()==1). Running tests together will accumulate documents across tests and cause flakiness.
Change fixture to function-scoped or explicitly clean collections between tests. Example:

@pytest.fixture(autouse=True, scope="function")
def clean_test_mongo():
    ...
    client.drop_database(db_name)
    yield
    client.drop_database(db_name)

Or drop collections at the start of each test to preserve session-level setup time if needed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed scope to "function"

Comment thread backend/scripts/run.sh

python manage.py wait_for_db || exit 1
python manage.py migrate --no-input || exit 1
python manage.py migrate --database=mongodb --no-input || exit 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script runs migrate --database=mongodb immediately after PostgreSQL migrations without ensuring the MongoDB service is ready. This can cause startup failures in environments where MongoDB takes longer to start.
Add a wait-for-mongo step (a management command or loop checking connection) before running the mongodb migrate, or rely on docker-compose depends_on with healthchecks. Example snippet:

# wait_for_mongo (pseudo)
python manage.py wait_for_mongo --timeout=60 || exit 1
python manage.py migrate --database=mongodb --no-input || exit 1

Or implement a simple loop using mongo --eval "db.adminCommand('ping')".

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added wait_for_mongodb command

@butuz0 butuz0 linked an issue Nov 17, 2025 that may be closed by this pull request
@butuz0 butuz0 requested a review from mehalyna November 17, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB schema & indexes for messages and conversations

2 participants