I have 2 models one is called User and the other Item. An array of items is assigned to each User(each user has 5 items). Through each user's session, the user can classify the items to Qualified or Not Qualified or none of them. Here is the Models:
const ItemSchema = new Schema({
title: {
type: String,
},
classification: {
type: String,
}
});
//Create the User Schema
const UserSchema = new Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
items: [ItemSchema],
});
UserSchema.pre("save", function (next) {
this.items = [];
this.items.push({
title: "Item 1",
classification: ""
});
this.items.push({
title: "Item 2",
classification: ""
});
this.items.push({
title: "Item 3",
classification: ""
});
next();
});
module.exports = User = mongoose.model("users", UserSchema);
As you can see the user's items has classification property which can be written "Qualified" or "Not Qualified" or "" in it. What I like to do is to count how many users classify Item 1 as "Qualified" and how many as "Not Qualified" & how many as "".So if 3 users classified Item 1 as "Qualified" and 2 users as "Not Qualified", I can say Item 1 is "Qualified".