I have a mongoose schema like this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const thingsSchema = new Schema({
userId: {
required: true,
unique: false,
type: String,
},
});
module.exports = mongoose.model('Thing', thingsSchema);
when trying to insert a new document gives me this error:
"E11000 duplicate key error index: thingsSchema.$userId_1 dup key: { : null }"
that error indicates that userId is unique, and I will insert duplicate because if it has been inserted before, but as you can see I specify unique: false
. The question is that before I had unique: true
, so if some were created with various userId
, but then changing to not be unique I kept giving that error, even after deleting the elements of the database .
Even if you remove the userId field, you still gave that error, which is the one that showed above, that as you can see { :null }
indicates that you are trying to create with value null
.
I've been using mongoose
and mongodb
a good time, and I never had this error. I hope you can help me. Thanks in advance.