first commit
This commit is contained in:
48
src/scripts/migrateCategoryIds.js
Normal file
48
src/scripts/migrateCategoryIds.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Product = require('../models/mongodb/Product');
|
||||
require('dotenv').config();
|
||||
|
||||
const migrateCategoryIds = async () => {
|
||||
try {
|
||||
// Connect to MongoDB
|
||||
await mongoose.connect(process.env.MONGODB_URI || 'your_mongodb_connection_string');
|
||||
console.log('Connected to MongoDB');
|
||||
|
||||
// Map old category IDs to new ones
|
||||
const categoryMapping = {
|
||||
'Clothing': 'cmiu33j770005141mz54xgsqe', // Fashion category
|
||||
'68c123e87e7f9a9b8b123456': 'cmiu34dfg0009141mn8r1dujd', // Men Clothing
|
||||
'68c123e87e7f9a9b8b123457': 'cmiu355i2000b141m2o7aqlb2', // Women Clothing
|
||||
'68c123e87e7f9a9b8b123458': 'cmiu3a7je000l141mwx9boup4', // Western Wear
|
||||
'68c123e87e7f9a9b8b123459': 'cmiu39ncw000j141mxizow1p2', // Lehengas
|
||||
'68c123e87e7f9a9b8b123460': 'cmiu384il000f141m6obcit4u', // Sarees
|
||||
'68c123e87e7f9a9b8b123461': 'cmiu39ncw000j141mxizow1p2', // Lehengas
|
||||
'68c123e87e7f9a9b8b123462': 'cmiu3cuwy000t141mkt4weuy5', // Ethnic Wear
|
||||
'68c123e87e7f9a9b8b123463': 'cmiu3a7je000l141mwx9boup4', // Western Wear
|
||||
'cmh4io0fv0001145t4057y8dw': 'cmiu34dfg0009141mn8r1dujd', // Men Clothing
|
||||
};
|
||||
|
||||
let totalUpdated = 0;
|
||||
|
||||
for (const [oldId, newId] of Object.entries(categoryMapping)) {
|
||||
const result = await Product.updateMany(
|
||||
{ category: oldId },
|
||||
{ $set: { category: newId } }
|
||||
);
|
||||
totalUpdated += result.modifiedCount;
|
||||
console.log(`✅ Updated ${result.modifiedCount} products from ${oldId} to ${newId}`);
|
||||
}
|
||||
|
||||
console.log(`\n🎉 Migration complete! Total products updated: ${totalUpdated}`);
|
||||
|
||||
// Close connection
|
||||
await mongoose.connection.close();
|
||||
console.log('MongoDB connection closed');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
migrateCategoryIds();
|
||||
Reference in New Issue
Block a user