57 lines
1.7 KiB
Markdown
57 lines
1.7 KiB
Markdown
# Application Detail Schema Changes
|
|
|
|
## Removed Fields
|
|
|
|
### createdDate Field (Removed)
|
|
- **Reason**: Redundant with MongoDB ObjectId timestamp
|
|
- **Storage Impact**: Saves ~8+GB storage (and 14+GB indexes) on billion+ documents
|
|
- **Index Impact**: Eliminates unnecessary index maintenance
|
|
|
|
## Migration Guide
|
|
|
|
### Getting Creation Time
|
|
Instead of using `createdDate`, extract timestamp from `_id`:
|
|
|
|
```javascript
|
|
// Old way:
|
|
const createdDate = appDetail.createdDate;
|
|
|
|
// New way:
|
|
const createdDate = appDetail._id.getTimestamp();
|
|
|
|
// Using utility function:
|
|
const utils = require('../helpers/utils');
|
|
const createdDate = utils.getDateTSFromObjectId(appDetail._id);
|
|
```
|
|
|
|
### Time-based Queries
|
|
Use ObjectId ranges for time-based filtering:
|
|
|
|
```javascript
|
|
const mongoose = require('mongoose');
|
|
|
|
// Create ObjectId from date
|
|
function createObjectIdFromDate(date) {
|
|
const timestamp = Math.floor(new Date(date).getTime() / 1000);
|
|
return new mongoose.Types.ObjectId(timestamp.toString(16) + '0000000000000000');
|
|
}
|
|
|
|
// Query by date range
|
|
const startObjectId = createObjectIdFromDate(startDate);
|
|
const endObjectId = createObjectIdFromDate(endDate);
|
|
|
|
const results = await AppDetail.find({
|
|
_id: { $gte: startObjectId, $lt: endObjectId }
|
|
});
|
|
```
|
|
|
|
## Updated Files
|
|
- `model/application_detail.js` - Removed createdDate field and index
|
|
- `scripts/cleanOrphanedAppDetails.js` - Updated to use _id.getTimestamp()
|
|
- `helpers/utils.js` - Added getDateTSFromObjectId() utility function
|
|
|
|
## Performance Benefits
|
|
- **Storage**: ~8+GB (and 14+GB indexes) saved on billion documents
|
|
- **Write Performance**: Reduced index maintenance overhead
|
|
- **Query Performance**: Uses existing _id index for time queries
|