File size: 1,037 Bytes
89ec743
 
 
 
 
 
bfde415
 
 
 
 
 
 
 
 
 
 
 
 
 
89ec743
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const mongoose = require('mongoose');

const connectDB = async () => {
    try {
        await mongoose.connect(process.env.MONGO_URI);
        console.log('✅ MongoDB ulandi');

        // SELF-HEALING: Drop obsolete index 'orderId_1' if it exists to prevent E11000 error
        try {
            const collection = mongoose.connection.collection('orders');
            const indexes = await collection.indexes();
            const obsoleteIndex = indexes.find(idx => idx.name === 'orderId_1');
            if (obsoleteIndex) {
                console.log("⚠️ Found obsolete index 'orderId_1'. Dropping...");
                await collection.dropIndex('orderId_1');
                console.log("✅ Obsolete index 'orderId_1' dropped successfully.");
            }
        } catch (dbErr) {
            console.error("⚠️ Index Check Error (Non-fatal):", dbErr.message);
        }
    } catch (err) {
        console.error('❌ MongoDB ulanishda xatolik:', err);
        process.exit(1);
    }
};

module.exports = connectDB;