Mongoose Aggregator Issue (Error: Arguments must be aggregate pipeline operators)

时间:2019-04-23 15:08:20

标签: mongoose

I have an issue with aggregation code in Mongoose : (Error: Arguments must be aggregate pipeline operators)

This is my code :

This is for get all dates of one month past in parameter of the function.

async function getDatesFromMonth(month) {
    Jour.aggregate([
        { 
            $project: {"month" : {$month: "$date"}}, 
            "date": "$date"
        },
        { 
            $match: {"month": month}
        } 
    ], (err,res)=>{
        if(err) {
            next(err);
        } else {
            console.log(res);
        }
    });
}

What i want (example) : When i pass 3 in parameters i want to get all the march dates of my db. :)

1 个答案:

答案 0 :(得分:0)

这是很好的代码。

async function getDatesFromMonth(month) {
    return Jour.aggregate([
        { 
            $project: {month : {$month: "$date"}, date: "$date"}, 
        },
        { 
            $match: {"month": month}
        },
        {
            $sort: {"date" :1}
        }
    ])
}