Integrating charts with data inside an array

I have a document set up like this:

name:
data: [
     {date: (ISO)
     numberCorrect: 8
     numberTotal: 10}
     {date: (ISO)
     numberCorrect: 9
     numberTotal: 10}
     {date: (ISO)
     numberCorrect: 4
     numberTotal: 5}
]

I want to create a chart that will show changes over time to the numberCorrect/numberTotal field. So, in this example, a line graph where the first entry is at 80% for that date, then the next date is 90%, then the next date is 80% again.

Can MongoDB charts do this or do I need to structure the data in a different way? I need to keep the # correct and # total, I can’t store as a straight percentage because sometimes totals over a period of time will also need to be calculated (in this example the actual average is 84%, but if I stored as percentages the average would calculate to 90%.)

How can I display the data in the way? Thank you!

Hi @Amber_Butler -

You should be able to do this. To get the percentage you need to use a calculated field, but the challenge here is that the values are within an array. Rather than use the array reductions on the encoding cards, you probably need to unwind the array in the query bar, i.e. using a query like:

[ { $unwind: '$data' } ]

This will result in one document per array element, so you should then be able to add a calculated field for numberCorrect / numberTotal and have it work as expected.

Tom

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.