删除MongoDB索引和缓存问题

由于数据模型是拷贝的,忘记删除原来的索引,导致存入数据时MongoDB抛异常exceptionHandler { MongoError: E11000 duplicate key error collection: house_eva.wpestatecomprehensivenesses index: real_estate_name_1_city_1 dup key: { real_estate_name: "胜利苑", city: null }

第一步:我删除模型的索引代码:// 创建复合唯一索引 WpEstateComprehensivenessSchema.index({ real_estate_name: 1, city: 1 }, { unique: true });上传代码。

第二步:删除所有索引,删除所有记录,查找所有索引并立即插入数据仍旧报相同的错误。

注意:MongoDB的索引可以删除。MongoDB没有具体的删除表的语句,MongoDB的删除表就实际相当于删除该表的所有记录,当然需要把后台的表模型代码删除。

javascript 复制代码
    // 删除索引
    WpEstateComprehensiveness.collection.dropIndex('real_estate_name_1_city_1', function(err) {
        if (err) {
            console.log('Error dropping index:', err);
        } else {
            console.log('Index successfully dropped!');
        }
    });
    // 删除 real_estate_name 为 "胜利苑" 的所有记录
    WpEstateComprehensiveness.deleteMany({ real_estate_name: '胜利苑' }, function(err) {
        if (err) {
            console.log('Error deleting documents:', err);
        } else {
            console.log('Documents successfully deleted!');
        }
    });
    WpEstateComprehensiveness.find({
        real_estate_name: '胜利苑',
        city: null
    }, function(err, duplicates) {
        if (err) {
            console.error('Error finding duplicates:', err);
        } else {
            console.log('Duplicates found:', duplicates);
            // Decide what to do with the duplicates (remove, update, etc.)
        }
    });
    // 获取所有索引信息
    WpEstateComprehensiveness.collection.indexInformation(function(err, indexes) {
        if (err) {
            console.log('Error getting index information:', err);
        } else {
            console.log('Indexes for WpEstateComprehensiveness:', indexes);
        }
    });

问题出在哪里呢?最后通过尝试,一步一步来,发现是换存问题。

解决步骤是:

第一次调用接口只删除索引。

第二次调用接口插入新数据。

千万不能两个一起使用,不然因为缓存问题导致删除索引失败的。

相关推荐
AAA修煤气灶刘哥11 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud15 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术18 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug1 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom1 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*1 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰1 天前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*1 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
-Xie-1 天前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存