I have a List model
const listSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'Users', required: true },
entries: [listEntry]
})
const List = mongoose.model('Lists', listSchema);
And the entry looks like this
const listEntry = new Schema({
_id: false,
book: { type: Schema.Types.ObjectId, ref: 'Books', required: true, unique: true },
note: { type: Number, default: null, max: 21 },
status: String,
history: [Object]
})
A listEntry can be modified whenever the user changes the note, the status, etc.
I wanted to push the history of every action the user does inside the listEntry but ALSO in the User
const userSchema = new Schema({
userId: { type: String, required: true },
history: [Object]
})
const User = mongoose.model('Users', userSchema);
The user history would contain ALL ListEntry's histories (all the actions of the account). A ListEntry's history would contain only the 'local' actions of the book.
I thought about using .post on the listSchema, but I don't know how to find the right ListEntry inside of it. The same would apply on a .post() on the UserSchema as I would need to go through the history.
Should I change my model modelisation ?
Solved by creating a single History model and using Virtuals in needed models.
By referencing the _id of the User as well as the List or any other component, I'm able to create this history.