I have these schema:
var userSchema = new mongoose.Schema({
username: {type: String,unique: true}
});
mongoose.model( 'User', userSchema );
var fooSchema = new mongoose.Schema({
title : {type: String,trim: true},
owner : {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});
fooSchema.index({ title: 1, owner: 1 }, { unique: true }); //does not work
mongoose.model( 'Foo', fooSchema);
I Want Use Validation, then each foo can have unique title with unique user
I try did
fooSchema.index({ title: 1, owner: 1 }, { unique: true });
But I Have an error also if another user create new foo with same title
UPDATE:
I added unique: true to username, but i have the same issue
For new viewers to this dormant thread:
mySchema.index({field1: 1, field2: 1}, {unique: true});
See this thread:
Creating Multifield Indexes in Mongoose / MongoDB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With