73 lines
No EOL
2.1 KiB
JavaScript
73 lines
No EOL
2.1 KiB
JavaScript
// MongoDB Shell Script for Price Tracking Migration
|
|
// Run with: mongosh mongodb://username:password@localhost:27017/stock price-migration.mongodb.js
|
|
|
|
print("Starting price tracking migration...");
|
|
|
|
const batchSize = 1000;
|
|
let processedCount = 0;
|
|
let cursor = db.eodSymbols.find({
|
|
lastPriceUpdate: { $exists: true },
|
|
'operations.price_update': { $exists: false }
|
|
});
|
|
|
|
let batch = [];
|
|
|
|
cursor.forEach(doc => {
|
|
// Normalize date to 00:00:00 UTC
|
|
const normalizedDate = new Date(doc.lastPriceUpdate);
|
|
normalizedDate.setUTCHours(0, 0, 0, 0);
|
|
|
|
// Parse lastPriceDate if it exists
|
|
let lastRecordDate = null;
|
|
if (doc.lastPriceDate) {
|
|
try {
|
|
lastRecordDate = new Date(doc.lastPriceDate);
|
|
} catch (e) {
|
|
print(`Failed to parse lastPriceDate for ${doc.Code}: ${doc.lastPriceDate}`);
|
|
}
|
|
}
|
|
|
|
batch.push({
|
|
updateOne: {
|
|
filter: { _id: doc._id },
|
|
update: {
|
|
$set: {
|
|
'operations.price_update': {
|
|
lastRunAt: normalizedDate,
|
|
lastSuccessAt: normalizedDate,
|
|
status: 'success',
|
|
...(lastRecordDate && { lastRecordDate })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (batch.length >= batchSize) {
|
|
db.eodSymbols.bulkWrite(batch);
|
|
processedCount += batch.length;
|
|
print(`Processed ${processedCount} documents...`);
|
|
batch = [];
|
|
}
|
|
});
|
|
|
|
// Process remaining batch
|
|
if (batch.length > 0) {
|
|
db.eodSymbols.bulkWrite(batch);
|
|
processedCount += batch.length;
|
|
}
|
|
|
|
print(`Migration completed. Processed ${processedCount} documents.`);
|
|
|
|
// Verify migration
|
|
const sampleDoc = db.eodSymbols.findOne({ 'operations.price_update': { $exists: true } });
|
|
print("\nSample migrated document:");
|
|
printjson(sampleDoc.operations);
|
|
|
|
// Count remaining documents with old fields
|
|
const remainingCount = db.eodSymbols.countDocuments({ lastPriceUpdate: { $exists: true } });
|
|
print(`\nDocuments still having lastPriceUpdate field: ${remainingCount}`);
|
|
|
|
// Optional: Remove old fields (uncomment to execute)
|
|
// print("\nRemoving old fields...");
|
|
// db.eodSymbols.updateMany({}, { $unset: { lastPriceUpdate: '', lastPriceDate: '' } });
|