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

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

#1

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:-


#2

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


#3

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.