删除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);
        }
    });

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

解决步骤是:

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

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

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

相关推荐
瀚高PG实验室17 分钟前
瀚高企业版V9.1.1在pg_restore还原备份文件时提示extract函数语法问题
数据库·瀚高数据库
TDengine (老段)27 分钟前
TDengine Tag 设计哲学与 Schema 变更机制
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
YOU OU1 小时前
Spring IoC&DI
java·数据库·spring
Muscleheng2 小时前
Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错
数据库·postgresql
罗超驿3 小时前
18.事务的隔离性和隔离级别:MySQL面试高频考点全解析
数据库·mysql·面试
jran-3 小时前
Redis 命令
数据库·redis·缓存
小江的记录本4 小时前
【Java基础】Java 8-21新特性:JDK21 LTS:虚拟线程、模式匹配switch、结构化并发、序列集合(附《思维导图》+《面试高频考点清单》)
java·数据库·python·mysql·spring·面试·maven
189228048614 小时前
NY382固态MT29F32T08GSLBHL8-24QM:B
大数据·服务器·人工智能·科技·缓存
June`4 小时前
多线程redis下如何解决aof重写和rdb持久化的数据一致性问题
数据库·redis·缓存
二宝哥4 小时前
离线安装maven
java·数据库·maven