update
This commit is contained in:
@@ -269,3 +269,68 @@ db.articles.find({ summary: { $exists: false } })
|
||||
// Count summarized articles
|
||||
db.articles.countDocuments({ summary: { $exists: true, $ne: null } })
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
## MongoDB Connection Configuration
|
||||
|
||||
### Docker Compose Setup
|
||||
|
||||
**Connection URI:**
|
||||
```env
|
||||
MONGODB_URI=mongodb://admin:changeme@mongodb:27017/
|
||||
```
|
||||
|
||||
**Key Points:**
|
||||
- Uses `mongodb` (Docker service name), not `localhost`
|
||||
- Includes authentication credentials
|
||||
- Only works inside Docker network
|
||||
- Port 27017 is NOT exposed to host (internal only)
|
||||
|
||||
### Why 'mongodb' Instead of 'localhost'?
|
||||
|
||||
**Inside Docker containers:**
|
||||
```
|
||||
Container → mongodb:27017 ✅ Works (Docker DNS)
|
||||
Container → localhost:27017 ❌ Fails (localhost = container itself)
|
||||
```
|
||||
|
||||
**From host machine:**
|
||||
```
|
||||
Host → localhost:27017 ❌ Blocked (port not exposed)
|
||||
Host → mongodb:27017 ❌ Fails (DNS only works in Docker)
|
||||
```
|
||||
|
||||
### Connection Priority
|
||||
|
||||
1. **Docker Compose environment variables** (highest)
|
||||
2. **.env file** (fallback)
|
||||
3. **Code defaults** (lowest)
|
||||
|
||||
### Testing Connection
|
||||
|
||||
```bash
|
||||
# From backend
|
||||
docker-compose exec backend python -c "
|
||||
from database import articles_collection
|
||||
print(f'Articles: {articles_collection.count_documents({})}')
|
||||
"
|
||||
|
||||
# From crawler
|
||||
docker-compose exec crawler python -c "
|
||||
from pymongo import MongoClient
|
||||
from config import Config
|
||||
client = MongoClient(Config.MONGODB_URI)
|
||||
print(f'MongoDB version: {client.server_info()[\"version\"]}')
|
||||
"
|
||||
```
|
||||
|
||||
### Security
|
||||
|
||||
- ✅ MongoDB is internal-only (not exposed to host)
|
||||
- ✅ Uses authentication (username/password)
|
||||
- ✅ Only accessible via Docker network
|
||||
- ✅ Cannot be accessed from external network
|
||||
|
||||
See [SECURITY_NOTES.md](SECURITY_NOTES.md) for more security details.
|
||||
|
||||
Reference in New Issue
Block a user