I recognized this question has been asked before, but none of the solutions seemed to work for me.
I have a simple model I'm defining like this:
const mongoose = require('mongoose')
const { Schema } = mongoose
let subscriberSchema = new Schema({
firstName: { type: String, required: true },
email: { type: String, required: true, unique: true }
}, { timestamps: true })
let Subscriber = mongoose.model('Subscriber', subscriberSchema)
If I run the following (in the REPL, so no async issues), I'd expect to see an error being logged for the second call to create
.
Subscriber.create({ firstName: "Landon", email: "[email protected]" })
Subscriber.create({ firstName: "Landon", email: "[email protected]" }, function(err) {
console.log("ERROR", err)
})
Instead, I see "ERROR" null
.
If I run a count
query or a find
query, I can see both models were created. What am I doing wrong?
Edit
Here are a few of the things I've already tried:
- Restart MongoDB after adding index
- Removing all of the existing records so there are no existing records that could violate the uniqueness constraint
- Defining the
email
attribute all of these ways (I've seen different implementations in different places):{ type: String, required: true, unique: true }
,{ type: String, required: true, index: true, unique: true }
,{ type: String, required: true, index: { unique: true } }
.
I recognized this question has been asked before, but none of the solutions seemed to work for me.
I have a simple model I'm defining like this:
const mongoose = require('mongoose')
const { Schema } = mongoose
let subscriberSchema = new Schema({
firstName: { type: String, required: true },
email: { type: String, required: true, unique: true }
}, { timestamps: true })
let Subscriber = mongoose.model('Subscriber', subscriberSchema)
If I run the following (in the REPL, so no async issues), I'd expect to see an error being logged for the second call to create
.
Subscriber.create({ firstName: "Landon", email: "[email protected]" })
Subscriber.create({ firstName: "Landon", email: "[email protected]" }, function(err) {
console.log("ERROR", err)
})
Instead, I see "ERROR" null
.
If I run a count
query or a find
query, I can see both models were created. What am I doing wrong?
Edit
Here are a few of the things I've already tried:
- Restart MongoDB after adding index
- Removing all of the existing records so there are no existing records that could violate the uniqueness constraint
- Defining the
email
attribute all of these ways (I've seen different implementations in different places):{ type: String, required: true, unique: true }
,{ type: String, required: true, index: true, unique: true }
,{ type: String, required: true, index: { unique: true } }
.
- Can you edit your question to include what you've checked out with the linked questions so that we don't have to ask those same questions? – JohnnyHK Commented May 15, 2016 at 1:45
- @JohnnyHK Sure, no problem. Updated. – LandonSchropp Commented May 15, 2016 at 8:03
3 Answers
Reset to default 5Actually, unique option for validation working,how?Follow these steps:
1)set unique: true for certain field in schema(example "email")
2)drop whole db
3)restart node server
4)test in postman and you will see than now works
Cheers.
This is very plicated thing to solve with node, as you know its async. If you are running both queries parallel, both will check if doc exists as same time and both will try to create the record.
There are two things you could do.
Create Unique Index
YourModel.index({ email: 1}, { unique: true })
OR
Use Update with $setOnInsert
var pk = {email : 'your email'};
YourModel.update(pk, {
$setOnInsert : data
}, {upsert : true})
And Make sure the index exists in mongoDB.
Mongoose does not modify index on key set. First try to remove index from mongo shell.
db.collection.dropIndex({email : 1})
Restart node process and mongoose will now create index with unique constraint.
Make autoIndex: true while connecting to the database.
mongoose
.connect('connection url', {
useUnifiedTopology: true,
useNewUrlParser: true,
autoIndex: true, //make this true
})
.then(() => {
console.log('Connected to mongoDB');
});