Why MongoDB?
Yato requires a database to store:- User data: AniList profile links and user preferences
- Guild configurations: Server-specific settings and preferences
- Token data: Random tokens for various features
- Game server data: Counter-Strike server configurations
Setup options
You can use MongoDB in several ways:Choose your setup method
Select the MongoDB setup that best fits your needs:
- MongoDB Atlas (recommended for production): Free cloud-hosted MongoDB
- Local MongoDB: Run MongoDB on your development machine
- Docker: Containerized MongoDB for easy local development
MongoDB Atlas (Cloud) - Recommended
MongoDB Atlas offers a free tier perfect for Discord bots.Create MongoDB Atlas account
- Go to MongoDB Atlas
- Sign up for a free account
- Create a new project (e.g., “Yato Bot”)
Create a cluster
- Click “Build a Database”
- Select the FREE tier (M0 Sandbox)
- Choose a cloud provider and region (closest to your bot’s server)
- Name your cluster (e.g., “yato-cluster”)
- Click “Create Cluster” (takes 3-5 minutes)
Create database user
- Go to “Database Access” in the left sidebar
- Click “Add New Database User”
- Choose “Password” authentication
- Create a username and strong password (save these!)
- Set privileges to “Read and write to any database”
- Click “Add User”
Configure network access
- Go to “Network Access” in the left sidebar
- Click “Add IP Address”
- For development: Click “Allow Access from Anywhere” (0.0.0.0/0)
- For production: Add your server’s specific IP address
- Click “Confirm”
For enhanced security in production, only whitelist your server’s IP address instead of allowing access from anywhere.
Get connection string
- Go to “Database” in the left sidebar
- Click “Connect” on your cluster
- Select “Connect your application”
- Copy the connection string
- Replace
<password>with your database user’s password - Replace
<dbname>with your database name (e.g.,yato)
Local MongoDB installation
For local development, you can install MongoDB directly on your machine.macOS
Windows
- Download MongoDB Community Server from mongodb.com/try/download/community
- Run the installer (use default settings)
- MongoDB will run as a Windows service automatically
Linux (Ubuntu/Debian)
Connection string for local MongoDB
Add this to your.env file:
Docker setup
Use Docker Compose for easy local MongoDB setup.Database schema
Yato automatically creates collections based on Mongoose models defined insrc/models/.
User model
Stores user data and AniList profile associations (src/models/user.js):
CSS model
Stores Counter-Strike server configurations (src/models/css.js).
Collections are created automatically when data is first written. You don’t need to manually create them.
Connection handling
The bot connects to MongoDB during startup insrc/index.js:11-13:
Connection options explained
useNewUrlParser: true: Uses the new MongoDB connection string parseruseUnifiedTopology: true: Uses the new Server Discover and Monitoring engine
These options were required for Mongoose 5.x (used by Yato). Newer versions of Mongoose (6.x+) don’t require these options.
Verifying the connection
When the bot starts successfully, you should see:Database management tools
Use these tools to view and manage your MongoDB data:MongoDB Compass (Recommended)
Official GUI for MongoDB:- Download from mongodb.com/products/compass
- Connect using your
MONGO_URIconnection string - Browse collections, documents, and run queries
MongoDB Atlas Dashboard
For Atlas users:- Log into MongoDB Atlas
- Click “Browse Collections” on your cluster
- View and edit data directly in the browser
VS Code extension
Install the “MongoDB for VS Code” extension:- Search for “MongoDB” in VS Code extensions
- Install “MongoDB for VS Code” by MongoDB
- Connect using your connection string
- Browse and query data without leaving VS Code
Troubleshooting
Authentication failed
Error:MongoServerError: Authentication failed
Solutions:
- Verify username and password in connection string
- Check that database user exists in Atlas
- Ensure password doesn’t contain special characters (URL encode if needed)
- For Atlas, verify IP whitelist includes your IP
Connection timeout
Error:MongoServerSelectionError: connection timeout
Solutions:
- Check internet connection
- Verify MongoDB service is running (local installations)
- For Atlas, check network access whitelist
- Ensure firewall allows outbound connections on port 27017
Cannot connect to localhost
Error:MongoNetworkError: failed to connect to server [localhost:27017]
Solutions:
- Ensure MongoDB service is running locally
- Check MongoDB is listening on port 27017
- Try
127.0.0.1instead oflocalhost - For Docker, ensure container is running:
docker ps
Database not found
MongoDB creates databases automatically when you first write data. If the database doesn’t exist yet, it will be created when the bot first saves data.
Production best practices
- Use MongoDB Atlas: More reliable than self-hosted solutions
- Enable authentication: Always require username/password
- Restrict network access: Whitelist only your server’s IP
- Regular backups: Enable automated backups in Atlas or set up backup scripts
- Monitor performance: Use Atlas monitoring or set up alerts
- Use connection pooling: Mongoose handles this automatically
- Handle connection errors: Implement reconnection logic for production
- Secure connection strings: Store
MONGO_URIin environment variables, never in code
Next steps
Now that MongoDB is configured:- Set up your environment variables
- Configure your Discord bot application
- Start the bot and verify the connection