MongoError: E11000 duplicate key error collection: timeformationBD.formations index: description_1 dup key: { : null }

I am getting duplicate key error while doing the POST action. I don’t want it to be unique.

error : MongoError: E11000 duplicate key error collection:
timeformationBD.formations index: description_1 dup key: { : null }

This is my formations route:-

const express = require('express');
const router = express.Router();
const Formation = require('../models/formations');
const config = require('../config/database');

router.get('/', function(req, res) {
  Formation.getFormations(function(err, formations) {
    if (err) throw err;
    res.json(formations);
  });
});

router.post('/', function(req, res) {
  let newFormation = new Formation({    
    name: req.body.name,
    email: req.body.email,
    username: req.body.username,
    password: req.body.password
  });
  Formation.addFormation(newFormation, function(err, formation) {
    if (err) throw err;
    res.json(formation);
  });
});
    
router.put('/:_id', function(req, res) {    
  let update = new Formation({
    name: req.body.name,
    email: req.body.email,
    username: req.body.username,
    password: req.body.password
  });
  Formation.updateFormation(req.params._id, update, function(err, formation) {
    if (err) throw err;
    res.json(formation);
  });
});

router.get('/:_id', function(req, res) {
  Formation.getFormation(req.params._id, function(err, formation) {
    if (err) throw err;
    res.json(formation);
  });
});

router.delete('/:_id', function(req, res) {
  Formation.deleteFormation(req.params._id, function(err, formation) {
    if (err) throw err;
    res.json(formation);
  });
});
    
module.exports = router;

This is my formations models:-

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const config = require('../config/database');    
const FormationSchema = mongoose.Schema({
  name: {
    type: String
  },
  email: {
    type: String
  },
  username: {
    type: String
  },
  password: {
    type: String
  } 
});

const Formation = module.exports = mongoose.model('Formation', FormationSchema);    

module.exports.getFormations = function(callback) {
  Formation.find(callback);    
}

module.exports.addFormation = function(newFormation, callback) {
  Formation.create(newFormation, callback);    
}

module.exports.updateFormation = function(callback) {
  Formation.findByIdAndUpdate(id, newFormation, callback);    
}

module.exports.deleteFormation = function(callback) {
  Formation.findByIdAndRemove(id, callback);    
}

module.exports.getFormation = function(callback) {
  Formation.findById(id, callback);    
}  

This is ARC client Screenshot:-

1 Like

I am also having this problem. Any help would be appreciated

The reason behind this error is that. The index is not present in your collection, in which you are trying insert. So Solution is to drop that collection and run your program again.

16 Likes

Thank you @Abhishek2kr, you saved my life!

Thank you so much!!!

Thanks I was getting so frustrated.

Drop Formation collection from DB and try again! Cheers

Thank you Abhishek2kr

Thank you so much. It was very helpful.

Thanks, man!!! Very useful

God bless you! It’s been hours since i’m struggling with this issue!

Can you believe I registered on this site just to say “thanks” to you!
Thanks a lot

1 Like

Thank you for the answer, but can this be fix without dropping the collection?
@Abhishek2kr

thanks bro you saved my life lol