Reputation: 1563
While building a MongoDB database for the first time I'm running into a few DB design problems.
From everything I read on this topic it's a good practice to add embedded documents when data is specific to this document. So here's my current problem:
I have a User Collection. Every user may have an Address. Since the address is specific to the user I'd say this should be an embedded document to my user collectionr, right?
I also want to store a Role (e.g. "user", "mod", "admin") - and since I have many different roles in my project a role also has a RoleType (e.g. "userType")
If I'd save these kinds of data to my user collection as well it would lead into:
in SQL I'd just go ahead and make a table ROLE_TYPE and table ROLE with a FK to ROLE_TYPE. Might this be for mongoDB in this case the way to go as well or should I really put everything into the user collection?
Upvotes: 0
Views: 259
Reputation: 4238
You have the right idea and even if it seems like you are bloating your User schema it is much more maintainable in the long run than having many relationship tables, here is an example how your User schema could look:
var ROLES = ['user', 'mod', 'admin'];
var UserSchema = new Schema({
email: {type: String},
address: {
address: {type: String},
city: {type: String},
zipCode: {type: Number},
country: {type: String},
},
role: {type: String, enum: ROLES},
})
Here you can see I've embedded the address field as a nested document. I've also utilized the Mongoose's enum attribute which says that only the roles in the ROLES array are permitted. If you attempt to save a role which is not in the list it will return an error.
But, if you meant that each role should also have a type I would definitely move this to a separate schema like this:
var ROLES = ['user', 'mod', 'admin'];
var RoleSchema = new Schema({
name: {type: String, enum: ROLES},
type: {type: String},
});
And link it to your UserSchema:
var UserSchema = new Schema({
...
role: {type: Schema.Types.ObjectId, ref: 'Role'},
})
Now when you are saving a new User you need to find the corresponding Role object and save the id of that object to the user, here is an example:
Role.findOne({name: 'user', type: 'userType'}, function(err, role) {
var user = new User({
email: '[email protected]',
address: {
address: '500 Wall St',
city: 'Seattle',
zipCode: 98121,
country: 'USA',
},
role: role._id,
})
})
Hope that helped, let me know if you have any other issues.
Upvotes: 1