Quiz Entry - updated: 2026.06.20
How should user accounts be stored in MongoDB?
Store each account as a document with at least login and hash, backed by a unique index on login.
const users = db.collection('users');
await users.createIndex({ 'login': 1 }, { unique: true });
Document structure:
{
login: "anna",
hash: "$2y$10$bsD5ralzGlwENSF1JY3..."
}
The unique index:
- Prevents duplicate usernames
- Speeds up login queries
- Returns error on duplicate insert