A comprehensive audit logging system for MongoDB that automatically tracks and logs every document change in real-time using MongoDB Change Streams and Database Triggers.
- Change Detection: When any document in your MongoDB database is modified (insert, update, delete, replace), MongoDB's Change Stream automatically detects the change
 - Trigger Activation: The database trigger (
Trigger.js) is instantly activated with the change event details - Field Analysis: The trigger analyzes the change at the field level, comparing old vs new values
 - Audit Log Creation: For each changed field, a separate audit log entry is created with complete metadata
 - Storage: Audit logs are stored in dedicated collections (e.g., 
users_logsforuserscollection) - Web Interface: The Next.js dashboard provides real-time access to view, search, and analyze all audit data
 
For Each Changed Field:
- Document ID of the affected record
 - Operation Type (INSERT, UPDATE, DELETE, REPLACE)
 - Changed Filed (including nested fields)
 - Old value (before change)
 - New value (after change)
 - Updated By (User who made the change)
 - Exact timestamp of the change
 
Example: When a user updates their email from old@email.com to new@email.com, the system creates:
{
  "documentId": "507f1f77bcf86cd799439011",
  "operationType": "update",
  "changedFields": "email",
  "oldValue": "old@email.com",
  "newValue": "new@email.com",
  "updatedBy": "admin@email.com",
  "timestamp": "2025-06-12T10:30:00.000Z"
}What You'll See:
- Create User Tab: Form to add new users with name, email, role, and complete address
 - Manage Users Tab: Table showing all users with edit/delete buttons
 - Real-time Updates: User list automatically refreshes after any operation
 
What Gets Audited:
- Every user creation, update, and deletion
 - Field-level changes (name, email, role, address components)
 
What You'll See:
- Collection Selector: Dropdown to choose which collection's logs to view
 - Search & Filters:
- Global search across all fields
 - Document ID filter
 - Changed field filter
 
 - Document Summary Table: Shows documents that have been modified with:
- Document ID (clickable for detailed history)
 - Last operation performed
 - Last modification timestamp
 
 
How to Use:
- Select a Collection from the dropdown
 - Use filters to narrow down results
 - Click any document ID to see detailed change history
 
What You'll See:
- Document Information: Document ID
 - Complete Change Timeline: Chronological table showing:
- Operation Type (INSERT/UPDATE/DELETE)
 - Changed Field
 - Previous Value
 - New Value
 - Updated By
 - Timestamp
 
 
How to Access:
- Click any document ID in the logs page
 - Or navigate directly: 
/document/[documentId]?collection=[collectionName] 
# Clone the repository
git clone https://github.com/sujeethshingade/mongodb-document-trigger.git
cd mongodb-document-trigger
# Install dependencies
npm install
# Create environment file
touch .env.local# Edit .env.local with your credentials
MONGODB_URI=your_mongodb_connection_string
# MongoDB Atlas API Configuration for Triggers Setup
ATLAS_PUBLIC_KEY=your_atlas_public_key_here
ATLAS_PRIVATE_KEY=your_atlas_private_key_here
ATLAS_PROJECT_ID=your_atlas_project_id_here
ATLAS_APP_ID=your_atlas_app_id_hereHow to Get Atlas Credentials?
- API Keys: Atlas → Access Manager → Project Access → Applications → API Keys → Create a new key
 - Project ID: Atlas → Project Settings
 - App ID: Atlas → App Services → Create App Service → App Settings
 
Option A: Automated Deployment (Recommended)
# Deploy the audit function to Atlas App Services
npm run deploy-functions
# Create triggers automatically from triggers.json
npm run setup-triggersConfigure Triggers for Your Collections:
Edit triggers/triggers.json to match your collections:
[
  {
    "name": "trigger_name",    // Adjust trigger name as requried
    "type": "DATABASE",
    "disabled": false,
    "function_name": "auditLogFunction",
    "config": {
      "operation_types": ["INSERT", "UPDATE", "DELETE", "REPLACE"],
      "database": "your_database_name",    // Adjust database name as requried
      "collection": "collection_name",                 // Adjust collection name as requried for trigger to be setup on
      "service_name": "mongodb-document-trigger",
      "match": {},
      "full_document": true,
      "full_document_before_change": true
    }
  }      // Add more trigger objects for additional collections
]Option B: Manual Setup via Atlas Dashboard
- 
Create App Service:
- Atlas → App Services → Create App Service
 - Choose your cluster and database
 - Note the App ID for your 
.env.local 
 - 
Deploy Function:
- App Services → Functions → Create New Function
 - Function Name: 
auditLogFunction - Copy contents from 
triggers/auditLogFunction.js - Save and Deploy
 
 - 
Create Triggers:
- App Services → Triggers → Add Trigger
 - Trigger Details:
- Trigger Type: Database
 - Watch Against: Collection
 - Cluster Name: Select your cluster
 - Database Name: Select your database
 - Collection Name: Select the collection to monitor
 - Operation Type: Select all (Insert, Update, Delete, Replace)
 - Full Document: ✅ Yes
 - Document Preimage: ✅ Yes (REQUIRED)
 
 
 - 
Function Configuration:
- Auto-Resume: ✅ Yes
 - Event Ordering: ✅ Yes
 - Event Type: Function
 - Function: Select 
auditLogFunctionor Copy and paste the entire contents ofTrigger.jsfrom this project and run - Trigger Name: Name the trigger
 - Save the trigger
 
 - 
Export Trigger App in App Services (Not Requried for Setup):
- App Services → Select Created App → Deployment → Export App → Download as a .zip file
 
 
# Start development server
npm run dev
# Or build for production
npm run build
npm startAccess the application at: http://localhost:3000
The triggers/ directory contains:
auditLogFunction.js- Main audit logging functiontriggers.json- Trigger definitions for bulk creationsetup-triggers.js- Automated trigger creation scriptdeploy-functions.js- Function deployment script
M0 (Free Cluster) Limitations:
- Auto-Resume: Not supported for bulk creation of triggers on M0 clusters (works on individual creation). Database triggers cannot automatically resume after failures.
 - Change Stream Options: Implement Change Stream options not available on M0 clusters. Cannot set the retention time for pre- and post-images in change streams for requried time.