3

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

numbers1311407
  • 33,686
  • 9
  • 90
  • 92
Barno
  • 3,271
  • 5
  • 28
  • 58
  • It seems like what you really want here is a unique compound of "title" and "username". Since you have chosen a referenced design as opposed to embedded the actual data is in separate collections. This makes the task impossible other than to specify "username" as unique to that model, which you probably want. – Neil Lunn Jun 18 '14 at 13:16
  • @NeilLunn Why i have to add username unique? ObjectId of userSchema is unique – Barno Jun 18 '14 at 13:21
  • Because when you add "Brad" and then "Brad" again as a username it will get a different ObjectId value each time. Unless "username" is actually unique. Derived case but you should see the point. – Neil Lunn Jun 18 '14 at 13:28
  • with `username` unique i have the same issue – Barno Jun 18 '14 at 13:33
  • You will if you append to your referenced array before trying to save to your "User" model. That is the point of failure you need to be looking for. – Neil Lunn Jun 18 '14 at 13:35
  • 1
    `unique` doesn't validate in the same way as other validations, rather it does what you're experiencing and returns the E11000 duplicate error from mongo. If you want to validate uniqueness before save you'd need to look for duplicate documents in a `pre` filter. [This question](http://stackoverflow.com/questions/13580589/mongoose-unique-validation-error-type) demonstrates how, or there's a [plugin](https://github.com/blakehaswell/mongoose-unique-validator) encapsulating the pattern. – numbers1311407 Jun 18 '14 at 13:35
  • For clarity, the fact that you're using a referenced approach is irrelevant here. Whether you're validating a single or compound index, mongoose does not validate uniqueness natively. – numbers1311407 Jun 18 '14 at 13:44
  • @numbers1311407 thanks. if i do `userSchema.index({ username: 1, email: 1 }, { unique: true });` for my userSchema it's works. Why for fooSchema Doesen't works? [http://stackoverflow.com/questions/13580589/mongoose-unique-validation-error-type] `index` is like `ensureindex` of Mongo, the I create a unique key – Barno Jun 18 '14 at 13:51

1 Answers1

3

For new viewers to this dormant thread:

mySchema.index({field1: 1, field2: 1}, {unique: true});

See this thread:

Creating Multifield Indexes in Mongoose / MongoDB

Community
  • 1
  • 1