1 year ago
#239510
francis
Mongoose unique sparse index in nested array
For my project, I am trying to allow users to use multiple emails or phone numbers.
My schema look like this:
const username = {
type: String,
trim: true,
sparse: true,
unique: true,
lowercase: true
}
var userSchema = Schema({
name : { type : String },
emails: [username],
phones: [username],
})
I used sparse
because I want users to use email/phone when signing up.
Am getting the following error when a second user tries to signup without providing a phone number.
ERR MongoServerError: E11000 duplicate key error collection: app.users index: phones_1 dup key: { phones: undefined }
{
"_id" : ObjectId("6215c3d9ec72c9f46b23f6d3"),
"name" : "Test User",
"emails" : [
"test@gmail.com"
],
"phones" : [ ],
}
How can I setup unique sparse index in nested array?
mongodb
mongoose
mongoose-schema
unique-index
0 Answers
Your Answer