more work
This commit is contained in:
parent
7a99d08d04
commit
b87a931a2b
11 changed files with 595 additions and 183 deletions
73
price-migration.mongodb.js
Normal file
73
price-migration.mongodb.js
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// 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: '' } });
|
||||
Loading…
Add table
Add a link
Reference in a new issue