Filter and re-arrange results using node.js and mo

2020-07-24 05:22发布

问题:

I created a server using Node.JS and Express. Database is MongoDB and ORM is Mongoose. Here's what I have to do.

    {
        "_id": "5788fcf7bfecf0c135178418",
        "incomeDate": "2012-01-01T00:00:00.000Z",
        "name": "Sales",
        "type": "Sales one",
        "amount": 5500,
        "__v": 0
    }

This is the object I'm getting from database. I want to filter these objects by date ( actually by month). I'm going to display monthly income in a chart using Angular Charts. (https://jtblin.github.io/angular-chart.js/) -> Angular Bar chart.

So if there's no amount in a specific month, there shoud be zero (0) in the list. The list must be like this. there are 12 elements of the list. This is just an example.

[200,1200,10,0,124,542,1254,500,0,1254,36,87]

How can I do this in node.js and mongoose ? Have to filter using incomedate and do some code in node.js.....

回答1:

Call it like this:

var date = new incomeDate();

{$group: {date: date.getMonth()}

Hey run your codes and see what error you get and we work around to fix them.