Mongoose Aggregate null values Problems

Viewed 37

i tried mongoose aggregate

const indexProducts = await Product.aggregate(
    [
        {
            $sort: {salesNumber: -1}
        },
        {
            $group: {
                _id: "$categoryId",
                products: {$push: "$$ROOT"},
            },
        },
        {
            $project: {
                products: {
                    $slice: ["$products", 4]
                }
            }
        },
        {
            $sort: {_id: 1},
        }
    ]
);

it's return

[
    { _id: 1, products: [ [Object], [Object], [Object], [Object] ] },
    { _id: 2, products: [ [Object], [Object], [Object] ] },
    { _id: 3, products: [ [Object], [Object] ] },
    { _id: 4, products: [ [Object] ] },
    { _id: 5, products: [ [Object] ] },
    { _id: 7, products: [ [Object] ] },
    { _id: 10, products: [ [Object] ] }
]

There are 10 categories in total. (1~10) And there is no data in the sixth, eighth, and ninth. I want get All data with null

Like this.

[
    { _id: 1, products: [ [Object], [Object], [Object], [Object] ] },
    { _id: 2, products: [ [Object], [Object], [Object] ] },
    { _id: 3, products: [ [Object], [Object] ] },
    { _id: 4, products: [ [Object] ] },
    { _id: 5, products: [ [Object] ] },
    { _id: 6, products: null },
    { _id: 7, products: [ [Object] ] },
    { _id: 8, products: null },
    { _id: 9, products: null },
    { _id: 10, products: [ [Object] ] }
]

please help me.

0 Answers
Related