unique and index schema properties don't work!?
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.6.1
Node.js version
16 (lts)
MongoDB server version
5.0.5
Description
Using the unique and index schema properties don’t work.
Steps to Reproduce
const UserSchema = new client.mongoose.Schema({
email: {
type: client.mongoose.SchemaTypes.Email,
required: true,
index: true,
unique: true
},
})
// Create model
const User = client.mongoose.model("User", UserSchema)
const user1 = new User({foo@test.mail})
console.log('user', user)
const user2 = new User({foo@test.mail})
console.log('user', user)
Expected Behavior
I would expect a second index on property email
and it being unique.
Issue Analytics
- State:
- Created a year ago
- Comments:12
Top Results From Across the Web
Mongoose Unique index not working! - Stack Overflow
1 . Add unique: true to the attributes. let schema = new mongoose. · 2 . Drop the collection - for example role...
Read more >Mongoose Unique Not Working - DEV Community
The first- and best imho- is to use mongoose.connection.syncIndexes() to reconstruct the indexes of all collections to match all schemas (it ...
Read more >Mongoose Unique Not Working?! | Have Fun Learning
1. DB already messed up. If there are already duplicated emails in your collections, then the new constraints won't work. · 2. Index...
Read more >Unique is not working · Issue #5050 · Automattic/mongoose
Not a bug, you have to understand that unique is an index configuration option in your schema. If your 'users' collection doesn't have...
Read more >Indexes — MongoDB Manual
The unique property for an index causes MongoDB to reject duplicate values for the indexed field. Other than the unique constraint, unique indexes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
your code and screenshots do not really align, because the code shows only creation of the documents, not actually saving (
new Model()
just creates a new document without saving, to actually save it directly useawait Model.create()
, orawait doc.save()
afternew Model()
)aside from that, did you already try
await Model.syncIndexes()
?This issue was closed because it has been inactive for 19 days since being marked as stale.