Trying to do this in mongoose:
const blogPost = new Schema({
title: { type: String, unique: true },
slug: { type: String, unique: true },
//...
});
However, I get an error:
MongoError: E11000 duplicate key error collection: blog.articles index: slug_1 dup key: { : null }
Is this not allowed? I just want to make sure nobody creates a slug twice, and neither uses the same title twice.
Actually you are creating slug
with null
value and getting error with having multiple documents with the null
in the slug
field.
Use sparse
index to eliminate the null
values.
const blogPost = new Schema({
title: { type: String, unique: true, sparse: true },
slug: { type: String, unique: true, sparse: true },
//...
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments