I have a schema for an item and a user. I want to allow a user to like or dislike an item and I also want to store a ratings association on the item as well as the user.
var UserSchema = new Schema({
username : { type: String, required: true, index: { unique: true }
, likes : [{ type: Schema.ObjectId, ref: 'Item'}]
, dislikes : [{ type: Schema.ObjectId, ref: 'Item'}]
, ratings: [???]
});
var ItemSchema = new Schema({
name: { type: String},
, likes : [{ type: Schema.ObjectId, ref: 'User'}]
, dislikes : [{ type: Schema.ObjectId, ref: 'User'}]
, ratings: [???]
});
The user stores an item ref, and the item stores a user ref for likes/dislikes. I am not sure how to store ratings for as an attribute though, since I want both the user and the value they rated the item.
item.ratings.forEach(function(rating){
<%= rating.user.username %> gave <%= item.name %> a <%= rating.value %>.
});
I also want to get a list of items a user has rated along with the rating value:
user.ratings.forEach(function(rating){
<%= user.username %> has rated <%= rating.item.name %> and gave it a <%= rating.value %>
});
What should my "ratings" schema look like? Is it possible to store two values? a user object id and a rating value (integer) and have a collection of these?
The other problem I see with my method is that mongoose doesn't support deep populate yet, so I would have to either use a module for it (https://github.com/JoshuaGross/mongoose-subpopulate), that is largely un-tested or store it in a different manner that won't have more than one level of nesting, so I can get my data with .populate()
Any feedback is appreciated, as I'm new to noSQL and perhaps I'm overcomplicating this.