猫猫按字段排序嵌套数组

问题描述 投票:0回答:1

我有一个GameStatistics集合,我想按他们的得分对所有玩家DESC进行排序。

[所有玩家都在带有游戏的嵌套'activePlayers'数组中列出。每个玩家都有一个字段“点”(我在下面放置了代码和模式)。

似乎我缺少了一些东西,我已经尝试过使用aggregate(),但是也没有运气。

sort = async (req: Request, res: Response, next: NextFunction) => {

        const id = '0021900805';
        const stats = await GameStats.findById(id).sort({'vTeam.activePlayers.points': -1}).exec();
        return res.status(200).json(stats);

    }

该集合看起来像这样:

{
    "vTeam": {

        "activePlayers": [
            {
                "teamId": "1610612759",
                "firstName": "Lonnie",
                "lastName": "Walker IV",
                "points": "23",
                "assists": "0",
                "rebounds": "4",
                "fgp": "40.0",
                "to": "0",
                "stl": "3",
                "blk": "0"
            },
            {
                "teamId": "1610612759",
                "firstName": "LaMarcus",
                "lastName": "Aldridge",
                "points": "4",
                "assists": "3",
                "rebounds": "14",
                "fgp": "45.0",
                "to": "2",
                "stl": "1",
                "blk": "1"
            }
        ]
    },
    "_id": "0021900805",
    "createdAt": "2020-04-05T15:34:26.457Z",
    "vTeamScore": "114"
}
mongodb typescript sorting mongoose schema
1个回答
0
投票

根据mongoose's docs,您的解决方案是将1替换为-1

所以:const stats = await GameStats.findById(id).sort({'vTeam.activePlayers.points': -1}).exec();

© www.soinside.com 2019 - 2024. All rights reserved.