Skip to content

Scoping a unique index to a user / compound indexes #3

@TimFletcher

Description

@TimFletcher

I have an issue whereby each of my lead documents stores an email address and a reference to a user ID e.g.:

{
  userId: 1234,
  email: '[email protected]'
}

I need different users to be able to create lead documents with the same email address. But they shouldn't be able to create a lead with a duplicate email.

If I use the following in my schema:

{
  "email": {
    type: String,
    index: true,
    unique: true
  }
}

Then it doesn't respect the scoping of the index to the user so if a second user tries to create a lead with an email address already in use by a lead owned by another user, it fails validation.

I'm just wondering if it's possible to create a compound index as you would with the undocumented Collection._ensureIndex e.g.

Leads._ensureIndex(
  { userId: 1, email: 1 },
  { unique: 1 }
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions