'MongoDB Charts - Cumulative document growth over time
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gobind Deep Singh |
