【问题标题】:E11000 duplicate key error collection: reklamaswin.ads index: username_1 dup key: { : null } [duplicate]E11000 重复键错误集合:reklamaswin.ads 索引:username_1 重复键:{:null } [重复]
【发布时间】:2018-07-21 20:13:23
【问题描述】:

为什么会出现此错误? E11000 duplicate key error collection: reklamaswin.ads index: username_1 dup key: { : null } 当我想制作新广告时。我知道某处有重复,但我找不到问题出在哪里。这是我在项目中使用的模式和发往“/new”的发布请求。我正在创建一个新广告并将其存储在广告集合中,然后将其推送到 req.user 的 user.ads 中

router.post('/new', middleware.isLoggedIn, function(req, res) {
  upload(req, res, function(err) {
     if (err) {
        req.flash('error', err.message);
        return res.redirect('/new');
     }

     var ad = new Ad({
       banner: '/uploads/' + req.file.filename,
       url: req.user.url,
       paymentType: req.body.paymentType,
       transactionId: req.body.transactionId
     });

     ad.save(function(err, ad) {
       if (err) {
         req.flash('error', err.message);
         return res.redirect('/new');
       }

       User.findById(req.user._id, function(err, user) {
         if (err) {
           req.flash('error', err.message);
           return res.redirect('/new');
         }

         user.ads.push(ad);
         user.save(function(err, ad) {
           if (err) {
             req.flash('error', err.message);
             return res.redirect('/new');
           }
           req.flash('success', 'Successfully added new ad.');
           res.redirect('/');
         });
       });
     });
  });
});

这是广告架构。

var AdSchema = new mongoose.Schema({
  banner: String,
  likes: {
    type: Number,
    default: 0
  },
  url: String,
  paymentType: {
    type: String,
    default: 'free'
  },
  transactionId: String,
  sponsored: {type: Boolean, default: false}
});

这是用户架构。

var UserSchema = new mongoose.Schema({
  username: {
    type: String,
    trim: true
  },
  email: {
    type: String,
    trim: true,
    unique: true
  },
  password: String,
  joined: {
    type: Date,
    default: Date.now
  },
  siteRole: {
    type: String,
    default: 'user'
  },
  ads: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Ad'
    }
  ]
});

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:
    E11000 duplicate key error collection: reklamaswin.ads index: username_1 dup key: { : null }
    
    • 此错误表示集合reklamaswin.ads 上有一个username 索引
    • 索引似乎是唯一的,因此所有具有username:null 的文档都会产生唯一性冲突。
    • 解决此问题的一种方法是找出所有未设置username 的此类文档并设置它
    • 另外,您可以选择删除索引

    【讨论】:

      【解决方案2】:

      如果不知道您的收藏索引是什么,很难准确判断,但似乎有一个名为 username_1 的广告收藏索引。具有这种名称格式的索引通常由 Mongoose 在字段上设置 index: true 时创建。在这种情况下,广告架构上显然有一个 username 字段和 index: true

      此外,null 消息表明存在索引条目,该字段为 null - 因为该字段不再存在于 Ads 集合中,所以这是预期的。因此,任何新记录也将具有该字段的 null 值,因此会产生重复键错误。

      如果此假设成立,则解决方法是删除 Ads 集合中的 username_1 索引。

      【讨论】:

      • 您好,但我的广告收藏中没有 username_1index,您可以在顶部查看。
      • @AmirŠaran 提供的代码仅显示您当前的模式,而不是 mongoDB 实际拥有的模式。这将包括由以前版本或针对同一 mongoDB 实例运行的其他代码创建的索引和集合。如果您要直接检查 mongoDB 实例,那么您会看到这些索引。
      猜你喜欢
      • 2019-06-17
      • 2019-07-11
      • 2023-03-25
      • 2016-08-17
      • 2021-10-25
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多