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

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

解决步骤是:

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

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

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

相关推荐
夜泉_ly1 小时前
MySQL -安装与初识
数据库·mysql
qq_529835352 小时前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
月光水岸New4 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6754 小时前
数据库基础1
数据库
我爱松子鱼4 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo5 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser6 小时前
【SQL】多表查询案例
数据库·sql
Galeoto6 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)6 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231116 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql