Overview
The users package provides functions for creating and retrieving user records from MongoDB. All functions interact with theusers collection.
Source: backend/users/model.go
Functions
CreateNewUser
Creates a new user document in the MongoDB users collection.Method Signature
backend/users/model.go:24-30
Description
Instance method on theUser struct that inserts the user document into the database.
Usage
Return Value
Returns
nil on success, or an error if:- Database connection fails
- Duplicate email (unique constraint violation)
- Invalid document structure
- MongoDB write errors
Implementation Details
- Uses
configs.StoreRequestInDb()helper function - Stores in
userscollection - No validation performed at this level
Validation should be performed before calling this function (e.g., email format, password strength).
GetUserById
Retrieves a user by their unique ID.Function Signature
backend/users/model.go:32-36
Parameters
User’s unique identifier (UUID)
Return Value
Returns the User struct. If not found, returns an empty User struct (all fields are zero values).
Usage
Implementation Details
- Queries MongoDB
userscollection - Uses
_idfield for lookup - Error is intentionally ignored (returns empty struct on error)
- Uses
context.TODO()(should use timeout context in production)
GetUserByEmail
Retrieves a user by their email address.Function Signature
backend/users/model.go:47-54
Parameters
User’s email address
Return Values
The User struct if found
nilon successmongo.ErrNoDocumentsif user not found- Other MongoDB errors (connection, query errors)
Usage
Implementation Details
- Queries MongoDB
userscollection - Uses
emailfield for lookup - Returns proper error handling
- Uses
context.TODO()(should use timeout context in production)
Common Use Cases
- Login authentication - Verify email exists before password check
- Registration validation - Check if email is already registered
- Password reset - Verify email for password reset flow
GetUserByPhone
Retrieves a user by their phone number.Function Signature
backend/users/model.go:38-45
Parameters
User’s phone number
Return Values
The User struct if found
nilon successmongo.ErrNoDocumentsif user not found- Other MongoDB errors (connection, query errors)
Usage
Implementation Details
- Queries MongoDB
userscollection - Uses
phonefield for lookup - Returns proper error handling
- Uses
context.TODO()(should use timeout context in production)
Common Use Cases
- Phone-based login - Alternative authentication method
- SMS verification - Lookup user for SMS OTP delivery
- Phone uniqueness check - Prevent duplicate phone registrations
Database Context
All functions usecontext.TODO() for MongoDB operations. For production applications, consider using context.WithTimeout():
Error Handling Patterns
GetUserById (No Error Return)
GetUserByEmail / GetUserByPhone (With Error Return)
MongoDB Queries
All query operations use MongoDB’s native query format (bson.M):
| Function | MongoDB Query |
|---|---|
| GetUserById | {"_id": "<id>"} |
| GetUserByEmail | {"email": "<email>"} |
| GetUserByPhone | {"phone": "<phone>"} |
Performance Considerations
Indexes
For optimal performance, create indexes on frequently queried fields:Query Optimization
_idqueries are fastest (primary key)- Email queries benefit from unique index
- Phone queries benefit from index
- Consider compound indexes for complex queries
Complete Usage Example
User Registration Flow
User Login Flow
Related Documentation
- User Model - User struct definition
- POST /register - Registration endpoint using CreateNewUser
- POST /login - Login endpoint using GetUserByEmail