question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unique and index schema properties don't work!?

See original GitHub issue

Prerequisites

  • 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)

screenshot screenshot

Expected Behavior

I would expect a second index on property email and it being unique.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Sep 20, 2022

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 use await Model.create(), or await doc.save() after new Model())

aside from that, did you already try await Model.syncIndexes()?

0reactions
github-actions[bot]commented, Nov 22, 2022

This issue was closed because it has been inactive for 19 days since being marked as stale.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found