MongoDB Charts - Cumulative document growth over time

485 Views Asked by At

I have an atlas cluster and I connected charts to it. I have zero experience with data visualizations (or building dashboards) and I'd like to chart the number of documents in my database over time. E.g., if in January 10 documents were created, and in February another 10 were created, then the chart should display 10 for January and 20 for February.

I know the _id field has information when the document was created, but I have no idea how to display it in the chart. So far, all I have is document count using _id in the y-axis.

What is the best way to accomplish what I'm trying to do? Is using a bar chart the best visualization to use in this situation?

Also, for some reason whenever I'm creating a chart, I get alerts saying "Query Targeting: Scanned Objects / Returned has gone above 1000". Is there any way to stop this for happening?

2

There are 2 best solutions below

0
MaxxABillion On BEST ANSWER

Basically you need to do the following:

  1. Since you want to treat the _id field as a date, you need to click the “…” button next to the _id field and choose Convert Type, then choose Date.

  2. Now make sure you have a Column chart selected

  3. Drag the _id field (which should now show as a date) onto the X axis. Keep the “Binning” toggle on and choose the binning period you want (Month is the default, and seems to be what you want).

  4. Now also drag the _id field to the Y axis. Keep the default COUNT aggregation selected. The chart should now show the number of documents created each month.

  5. Finally, enable the “Compare Periods” toggle and leave the deault “Cumulative Total” option selected.

Below is what the results "should" look like.

enter image description here

0
Gobind Deep Singh On

There are 2 parts to the question. I will answer each of them independently.

First Regarding Displaying The Cart

You are right that _id has all the information, it is encoded. So you will have to decode it to fetch those attributes. This might get very complicated very soon given we have to perform this in an aggregation pipeline.

I would rather recommend introducing a field called created_at which would be the timestamp generated when the document is inserted. Then all you need is a simple $group on created_at with count. And that would display data just fine (Bar Graph or Line Graph should suffice in the case).

Second - The alert received from Atlas regarding Query Targeting Ratio

We first need to understand what that Alert means. It essentially means that the ratio of no. of documents being scanned from the disk vs no. of documents returned from the disk is going beyond 1000. This means you are scanning more than 1000 times extra records in the disk than you are required to - which can be interpreted as a poorly indexed query.

You can simply fix this by creating a supportive index for the $match filter you are using in the aggregation. If there is no $match then unfortunately you will have to live with the alert as no indexed will be picked in that case.