MongoDB Setup Options
You can set up MongoDB in two ways:- Local MongoDB - Install and run MongoDB on your development machine
- MongoDB Atlas - Use MongoDB’s cloud-hosted database service (recommended for production)
Local MongoDB Installation
Download MongoDB
Download MongoDB Community Server from MongoDB Download Center.Select the version appropriate for your operating system:
- Windows: MSI installer
- macOS: DMG package or Homebrew
- Linux: Package manager (apt, yum) or tarball
Install MongoDB
Follow the installation wizard for your platform.macOS with Homebrew:Ubuntu/Debian:Windows:
Run the MSI installer and follow the setup wizard.
Verify installation
Verify MongoDB is running:You should see the MongoDB shell prompt. Type
exit to quit.MongoDB Atlas Setup (Cloud)
MongoDB Atlas is recommended for production deployments and offers a free tier for development.Create MongoDB Atlas account
Sign up for a free account at MongoDB Atlas.
Create a new cluster
- Click “Build a Database” or “Create”
- Select “Shared” for the free tier (M0)
- Choose your cloud provider and region (select one closest to your users)
- Name your cluster (e.g.,
smartshelf-cluster) - Click “Create Cluster” (this may take a few minutes)
Configure database access
- Go to Database Access in the left sidebar
- Click “Add New Database User”
- Choose “Password” authentication
- Enter a username and auto-generate a secure password
- Save the credentials securely
- 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 specific IP addresses of your servers
- Click “Confirm”
Allowing access from anywhere (0.0.0.0/0) is convenient for development but should be restricted in production.
Get connection string
- Go to Database in the left sidebar
- Click “Connect” on your cluster
- Select “Connect your application”
- Choose “Node.js” as the driver and select the latest version
- Copy the connection string
Connection String Formats
Local MongoDB
MongoDB Atlas
Database Initialization
SmartShelf automatically creates collections and indexes when you first start the backend server. No manual database initialization is required.Automatic Collection Creation
When you start the backend, Mongoose will automatically:- Create the
smartshelfdatabase if it doesn’t exist - Create collections when the first document is inserted
- Build indexes defined in the models
Initial Data
To create an initial admin user, you can use the registration endpoint or create one directly in the database:Collections Overview
SmartShelf uses three main collections:Users Collection
Stores user accounts with role-based access control. Schema fields:name(String, required) - User’s full nameemail(String, required, unique) - User’s email addresspassword(String, required) - Hashed password using bcryptrole(String, required) - One of:Admin,Manager,WorkerisActive(Boolean) - Account statuscreatedAt,updatedAt(Date) - Timestamps
email- Unique index for fast lookups and authenticationrole- Index for role-based queries
Inventory Collection
Stores product inventory items with expiry tracking and stock management. Schema fields:productName(String, required) - Name of the productcategory(String, required, indexed) - Product categorysku(String, required, unique) - Stock Keeping Unit (format:XX-000)quantity(Number, required) - Current stock quantitypurchaseDate(Date, required) - Date item was purchasedexpiryDate(Date, required, indexed) - Product expiration datesupplier(String, required) - Supplier namecreatedBy(ObjectId, ref: User) - User who created the recordlastModifiedBy(ObjectId, ref: User) - User who last modifiedcreatedAt,updatedAt(Date) - Timestamps
daysUntilExpiry- Calculated days remaining until expiryisExpired- Boolean indicating if item is expiredisLowStock- Boolean for stock below threshold (10)stockStatus- String:Out of Stock,Low Stock,Medium Stock,In Stock
sku- Unique indexcategory- For category-based filteringexpiryDate- For FEFO ordering and expiry alertsquantity- For stock level queriescategory + expiryDate- Compound index for optimized queries
Tasks Collection
Stores work assignments for warehouse workers. Schema fields:description(String, required) - Task descriptionassignedTo(ObjectId, ref: User, required) - Worker assigned to taskassignedBy(ObjectId, ref: User, required) - Manager/Admin who created taskstatus(String) - One of:Pending,In Progress,CompletedcompletedAt(Date) - Completion timestampcreatedAt,updatedAt(Date) - Timestamps
duration- Time between creation and completionisOverdue- Boolean for tasks pending > 48 hours
assignedTo- For worker-specific task queriesstatus- For filtering by task statuscreatedAt- For chronological orderingassignedTo + status- Compound index for optimized queries
Database Management Tools
MongoDB Compass
MongoDB Compass is a GUI tool for visualizing and managing your database:- Download from MongoDB Compass
- Install and launch the application
- Connect using your connection string
- Browse collections, run queries, and view data visually
MongoDB Shell (mongosh)
Command-line interface for database operations:Production Considerations
- Enable authentication - Always use username/password authentication
- Restrict network access - Whitelist specific IP addresses only
- Use SSL/TLS - Enable encrypted connections (MongoDB Atlas uses this by default)
- Regular backups - Set up automated backups (Atlas provides automated backups)
- Monitor performance - Use MongoDB Atlas monitoring or third-party tools
- Index optimization - Review and optimize indexes based on query patterns
See the Deployment Guide for more production setup recommendations.