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

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

解决步骤是:

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

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

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

相关推荐
瓜酷月..3 小时前
MySQL的高可用+MHA
数据库·mysql
差不多的张三3 小时前
【解决方案】powershell自动连接夜神adb端口
数据库·adb
小马哥编程4 小时前
【软考架构】第6章 数据库基本概念
数据库·oracle·架构
自学也学好编程4 小时前
【数据库】PostgreSQL详解:企业级关系型数据库
数据库·postgresql
.Eyes6 小时前
OceanBase 分区裁剪(Partition Pruning)原理解读
数据库·oceanbase
MrZhangBaby7 小时前
SQL-leetcode— 2356. 每位教师所教授的科目种类的数量
数据库
一水鉴天7 小时前
整体设计 之定稿 “凝聚式中心点”原型 --整除:智能合约和DBMS的在表层挂接 能/所 依据的深层套接 之2
数据库·人工智能·智能合约
翔云1234567 小时前
Python 中 SQLAlchemy 和 MySQLdb 的关系
数据库·python·mysql
孙霸天8 小时前
Ubuntu20系统上离线安装MongoDB
数据库·mongodb·ubuntu·备份还原
Java 码农8 小时前
nodejs mongodb基础
数据库·mongodb·node.js