0

I don't know how to exactly explain my question in title. Sorry for that.

Here is what I mean: I have user model

var userSchema = new Schema({
    username: { type: String, required: true },
    messages: { type: Number, required: true, default: 0 },
    noticed: { type: Date, required: true, default: Date.now },
    chatId: { type: Number, required: true }
    })

username and chatId may be not unique, but there should be no users which username and chatId both are the same!

Meaning of this is that there can not be two users with the same username in one chat. But user may be in many different chats. How do I do it in mongoose?

PinkiNice
  • 1,455
  • 2
  • 13
  • 19

1 Answers1

1

I think this will work:

userSchema.index({ username : 1, chatId : 1 }, { unique : true });
robertklep
  • 198,204
  • 35
  • 394
  • 381