I hope this is ok here, and that mongoose counts as Nodejs.
I have a 'top level' schema, that isn't particularly important, but it needs to return and sum some data from my main documents (in a one to many relationship).
I'm struggling, because I want to create a virtual field that returns results based on the sum of a virtual field on my other schema. I could do this on the frontend, but although it's not that important it is part of the api, so obviously, that would be bad practice, and it feels to me like doing it as a virtual field on the schema is the correct way to go about it.
So this is the code, users select projects (my other schema), and add them to an array, like so....
This returns the correct sub-docs. But when I try and do my sum (a virtual field sum of a virtual field), it doesn't work.....
I'm a bit out of my depth here, I'm not sure if I need to populate the 'project' documents somewhere in there (if I check in my loop, the element is just the id ref), or if you can't create a virtual field that does calculations on another virtual field. There is probably something I can do in the aggregation pipeline, but I'm not sure exactly what.
I really want this to be as lightweight as possible, I feel like I'm missing something major that I can't think how to do this easily. I just want to return the subdocuments (to display them), and create a field(s) that totals up of some of the subdocument(s) data.
Can anyone put me out of my misery and give me a hint? Thanks.
I have a 'top level' schema, that isn't particularly important, but it needs to return and sum some data from my main documents (in a one to many relationship).
I'm struggling, because I want to create a virtual field that returns results based on the sum of a virtual field on my other schema. I could do this on the frontend, but although it's not that important it is part of the api, so obviously, that would be bad practice, and it feels to me like doing it as a virtual field on the schema is the correct way to go about it.
So this is the code, users select projects (my other schema), and add them to an array, like so....
Code
const programmeSchema = new mongoose.Schema({
name: {
type: String
},
projects:
[{ type: mongoose.Schema.ObjectId, ref: 'Project' }],
...
})This returns the correct sub-docs. But when I try and do my sum (a virtual field sum of a virtual field), it doesn't work.....
Code
programmeSchema.virtual('totalProgramme').get(function () {
let totalProgramme = 0
this.projects.forEach(function (el) {
totalProgramme = totalProgramme + el.total
})
return totalProgramme
})I'm a bit out of my depth here, I'm not sure if I need to populate the 'project' documents somewhere in there (if I check in my loop, the element is just the id ref), or if you can't create a virtual field that does calculations on another virtual field. There is probably something I can do in the aggregation pipeline, but I'm not sure exactly what.
I really want this to be as lightweight as possible, I feel like I'm missing something major that I can't think how to do this easily. I just want to return the subdocuments (to display them), and create a field(s) that totals up of some of the subdocument(s) data.
Can anyone put me out of my misery and give me a hint? Thanks.

