0

Why am I getting this error? E11000 duplicate key error collection: reklamaswin.ads index: username_1 dup key: { : null } when I want to make a new ad. I know that there is a duplicate somewhere, but I can't find where is the problem. Here are my schemas that I use in the project and the post request that goes to "/new". I'm creating a new Ad and storing it in the ads collection and then pushing it into the user.ads for the req.user

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('/');
         });
       });
     });
  });
});

This is the Ad schema.

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

This is the User schema.

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'
    }
  ]
});
Amir Šaran
  • 406
  • 1
  • 4
  • 14

2 Answers2

1
E11000 duplicate key error collection: reklamaswin.ads index: username_1 dup key: { : null }
  • This error says that there is a username index on collection reklamaswin.ads
  • The index seems to be unique hence all documents that have username: null creates a conflict in uniqueness.
  • One way to solve this is to find out all such documents with username is not set and set it
  • Also, you can drop the index optionally
Atish
  • 4,277
  • 2
  • 24
  • 32
0

It is hard to tell exactly without knowing what your collection indexes are but it appears there is an index for the Ads collection named username_1. Indexes with this name format are typically created by Mongoose when setting index: true on a field. In this case apparently there was a username field with index: true on the Ads schema at some point.

Also, the null message is indicating that an index entry exists where this field is null -- expected since the field no longer exists on the Ads collection. So any new records would also have a null value for this field and would therefore create a duplicate key error.

If this assumption is true then the fix would be to drop the username_1 index on the Ads collection.

Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • Hello, but I don't have a username_1index in my ads collection, you can see in the top. – Amir Šaran Feb 11 '18 at 16:00
  • @AmirŠaran The provided code only shows your current schema, not what the mongoDB actually has. This would include indexes and collections created by previous versions of or other code that was run against the same mongoDB instance. If you were to inspect the mongoDB instance directly then you would see these indexes. – Jason Cust Feb 11 '18 at 21:03