I want to use a Chart Type: Number to display only the percentage of gains.
I have a collection with a field named “result” that is filled with “gain” or “loss”
I need to count number of docs in total and number of docs with result = “gain” to calculate this percentage.
How can I do it? I tried a lot of queries and calculated fields but nothing works. Can anyone help me to do it?
This is possible but a little tricky. The secret is to use $facet which lets you fork the pipeline to calculate two different results (in this case, the number of gains, and the total number of documents). After this, we can combine the two results into a single figure:
As you probably figured out, you can’t use a normal Charts date filter since it doesn’t put it at the right point of the pipeline. However you should be able to do something like this: