How to create graphs or historigrams with mongoDB
I need to make a graph where to show how many total users we have registered in a time interval. (The language is TypeScript but I can get an answer in another language or using the aggregate of mongoDB)
Example:
Day 1: 10 total users registered
Day 2: 139 ...
Day 3: 1230 ...
Day 4: 2838 ...
...
...
Current day: X number of users ... and so it would end.
It should be noted that all users have a field called createAt, which is of type date.
I tried to obtain the users by means of cubes but it is not an optimal solution.
const response = await this.userModel.aggregate([
{
$bucketAuto: {
groupBy: '$createdAt',
buckets: 4,
},
},
]);
console.log(response);
I have also thought about using mapReduce from mongoDB and pass the specified function to it. But in terms of performance, I would like to know if that could create the pipelines simply with aggregate. mapReduce would be a second option (slightly slower) and as a last option to get all the users (only with the CreateAt field) and process them in my backend.
Thank you in advance for your answers
Update
I also mean that with autoBucket it orders them by non-specific time intervals, it basically orders them by the number of users and groups them by the creation dates, also when i passed the dates to mongodb, with $bucket the result is not as expected
input Example:
const list = [
{
"createdAt": "2021-08-30T23:47:16.663Z",
"_id": "612d6e044007a95446848cef"
},
{
"createdAt": "2021-08-31T04:18:11.820Z",
"_id": "612dad830541fa001bb63671"
},
{
"createdAt": "2021-08-31T04:18:47.794Z",
"_id": "612dada70541fa001bb63674"
},
{
"createdAt": "2021-08-31T04:20:14.415Z",
"_id": "612dadfe0541fa001bb63678"
},
{
"createdAt": "2021-08-31T04:22:45.580Z",
"_id": "612dae950541fa001bb63682"
},
{
"createdAt": "2021-08-31T11:24:28.471Z",
"_id": "612e116c0541fa001bb63688"
},
{
"createdAt": "2021-08-31T18:47:09.452Z",
"_id": "612e792dba2a3e1d081c9f3d"
}
];
from Recent Questions - Stack Overflow https://ift.tt/39vwPPJ
https://ift.tt/eA8V8J
Comments
Post a Comment