MongoDB - 2 Unique keys in Single Schema not allowed?

R. Kohlisch

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.

Ashh

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.

edited at
0

Comments

0 comments
Login to comment

Related

Is it a good idea to have unique keys to better aggregate data in MongoDB

Mongodb - group() can't handle more than 20000 unique keys

Comparing 2 lists consisting of dictionaries with unique keys in python

Is there a way to make the combination of two foreign keys in a single JPA entity unique?

Joining 3 x MYSQL Tables together with 2 Different Unique Keys

MongoDB - Trying to save multiple documents results in duplicate key error for a schema's ObjectId despite no unique key set

Reference multiple schema in a single schema

Make a field unique in one schema but not unique in other schemas with MongoDB/Mongoose

Query MongoDB with mongoose and return an array of string matching one of the keys of the schema

How to merge two array of objects by key AND keep unique keys in a single array of objects?

Are dashes allowed in dict keys in ansible?

Postgres constraint name need to be unique across single table or entire schema?

MongoDB aggregation - group in 2 ways in a single query

MongoDB: How to map faceted results into a single object of keys and values

In MongoDB, what are the actual limitations imposed by unique indexes on shard keys?

Retrieve the unique keys (second dimension) of a 2 dimensional dictionary in python?

"* is not allowed by the schema"

How to create a transformed Doctrine 2 entity that is based on several unique keys

Star schema, surrogate keys

Is it possible to merge 2 dataframe with no unique keys

Is there a way to relationship between two table which table one has 3 unique keys and table two has 2 unique keys in SQL Server?

XSD simple schema element not allowed

How do I set multiple unique indexes in schema in mongodb?

Nested JSON with unique keys to flatten DataFrame with first 2 levels keys in columns

MongoDB/Mongoose schema unique not working

Unique keys > 3072 bytes allowed in MariaDB versions > 10.5

From MongoDB Schema, how to extract its keys

mongodb count unique constraint on 2 fields

Could there be a (unique) workaround for duplicate "$or" keys when working with boolean logic in MongoDB?

TOP Ranking

HotTag

Archive