ToB企服应用市场:ToB评测及商务社交产业平台

标题: 删除MongoDB索引和缓存题目 [打印本页]

作者: 卖不甜枣    时间: 2024-6-11 12:59
标题: 删除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的删除表就实际相当于删除该表的全部记录,当然需要把背景的表模型代码删除。
  1.     // 删除索引
  2.     WpEstateComprehensiveness.collection.dropIndex('real_estate_name_1_city_1', function(err) {
  3.         if (err) {
  4.             console.log('Error dropping index:', err);
  5.         } else {
  6.             console.log('Index successfully dropped!');
  7.         }
  8.     });
  9.     // 删除 real_estate_name 为 "胜利苑" 的所有记录
  10.     WpEstateComprehensiveness.deleteMany({ real_estate_name: '胜利苑' }, function(err) {
  11.         if (err) {
  12.             console.log('Error deleting documents:', err);
  13.         } else {
  14.             console.log('Documents successfully deleted!');
  15.         }
  16.     });
  17.     WpEstateComprehensiveness.find({
  18.         real_estate_name: '胜利苑',
  19.         city: null
  20.     }, function(err, duplicates) {
  21.         if (err) {
  22.             console.error('Error finding duplicates:', err);
  23.         } else {
  24.             console.log('Duplicates found:', duplicates);
  25.             // Decide what to do with the duplicates (remove, update, etc.)
  26.         }
  27.     });
  28.     // 获取所有索引信息
  29.     WpEstateComprehensiveness.collection.indexInformation(function(err, indexes) {
  30.         if (err) {
  31.             console.log('Error getting index information:', err);
  32.         } else {
  33.             console.log('Indexes for WpEstateComprehensiveness:', indexes);
  34.         }
  35.     });
复制代码
题目出在哪里呢?最后通过尝试,一步一步来,发现是换存题目。
解决步调是:
第一次调用接口只删除索引。
第二次调用接口插入新数据。
千万不能两个一起利用,不然因为缓存题目导致删除索引失败的。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4