Creating a gauge with MongoDB Charts

The following is a sample of my doc:

I am hoping to create a gauge with

  1. Value: Number of Doc that has “installedOn” != null
  2. Target: Total Number of Doc that has “registeredOn” != null

For now, all my “registeredOn” are not null so there is no problem with my Target. However, I do not know how can I achieve 1? With “distinct”, there will be a count for each value of “installedOn”.

Edit: With “distinct” it gives a count to null as well. How can I edit the aggregation pipeline so that it doesnt take into account null?

Hi @KwanYee_Tan -

This is a little tricky, but fortunately not impossible. The COUNT aggregation will count the number of non-missing values, but it does include nulls. If you were able to reshape your data such that the values you don’t want to count are not in the documents at all, it would work just with COUNT.

Assuming you want to keep the data as it is, it is possible to use an aggregation pipeline to remove the null values. I started with the following documents:


When I build a gauge chart from this, I get 2 bars and 4 foos, which is the number of non-missing values (but including nulls):

The following pipeline in the query bar removes the null values:

  { "$project": {
    "foo": { "$cond": [{ "$eq": [ "$foo", null ] }, "$$REMOVE", "$foo" ] },
    "bar": { "$cond": [{ "$eq": [ "$bar", null ] }, "$$REMOVE", "$bar" ] }   

With that applied, I get the chart you are asking for.



Thank you so much!!! You are the best.

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