0

I have a Users collection and an Events collection. The users collection is unremarkable, here's my events Schema:

schema = new mongoose.Schema({
    user: {
            type        :  mongoose.Schema.ObjectId,
            ref         :  'User',
            required    :  true
          },
    name: {
             type       :  String,
             unique     :  true,
             required   :  true
          }
});

This works. I cannot create an event without a username and the name property is required and unique. Problem is, it's unique on the database. So if user A creates an event called "Meet up", no other user can create an event with that name. I want the "uniqueness" to be restricted by user, so User A cannot create two events named "Meet up", but user B can create one with that name.

How would I approach this?

enrique-ramirez
  • 914
  • 2
  • 11
  • 23
  • I'm thinking you'll need some kind of.. compound index that is unique. Here's the dox for 2.7 from a quick search: http://mongoosejs.com/docs/2.7.x/docs/indexes.html 4.0 doesn't include such an example, but i think it's the same. – Kevin B Jul 17 '15 at 22:18
  • @KevinB I'll check upon that. I'm unsure what compound indexes are or how to implement them, so it might take a while whilst I dive in. Will let you know how it goes. Thank you! – enrique-ramirez Jul 17 '15 at 22:38

0 Answers0