Skip to content

How do you handle references to other documents/tables? #16

@jarrodpayne

Description

@jarrodpayne

I've got a One-to-One relationship between a User model and a few role models such as Admin, Manager, and Customer. How might you handle a scenario like this? I'm using Mongoose, but I believe the same principles apply for any One-to-One relationship.

User Schema

var UserSchema = new Schema({
  email: {
    // ...
  },
  password: {
    // ...
  },
  roles: {
    admin: { type: mongoose.Schema.Types.ObjectId, ref: 'Admin' },
    manager: { type: mongoose.Schema.Types.ObjectId, ref: 'Manager' },
    customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer' }
  },
  salt: {
    // ...
  },
  updated: {
    // ...
  },
  created: {
    // ...
  }
});

Admin Schema

var AdminSchema = new Schema({
  user: {
    id: { type: Schema.Types.ObjectId, ref: 'User' },
    name: { type: String, default: '' }
  },
  created: {
    // ...
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions