简体   繁体   English

我如何计算 mongodb

[英]How do I count in mongodb

Currently I want to count the number of participant inside my Register collection for each of my Tournament collection.目前我想为我的每个锦标赛集合计算我的注册集合中的参与者数量。 Below is how I tried to count all the participant of each tournament, but its not showing the count.以下是我如何尝试计算每场比赛的所有参与者,但它没有显示计数。 How do I resolve this?我该如何解决这个问题? Am I doing it wrongly?我做错了吗?

router.get('/:id', function(req, res){
    Tournament.findById(req.params.id, function(err, tournament){
        User.findById(tournament.author, function(err, user){
            Register.find({tournamentId: req.params.id}, function(err, registers){
                if(err){
                    console.log(err);
                    return;
                }
                else{
                    res.render('tournament',{
                        tournament: tournament,
                        author: user.username,
                        user: req.user,
                        registers: registers
                    });
                }
            }).count(function(err, register){
                if(err){
                    console.log(err);
                    return;
                }
                else{
                    res.render('tournament',{
                        register: register
                    });
                }
            });
        });
    });
});
strong Participants List (Count Number of Participant: #{register})
                        br
                        br
                        br
                        table.table
                            thead
                                tr
                                th(scope='col') Register Id
                                th(scope='col') Full Name
                                th(scope='col') Contact Number
                                th(scope='col') Username
                                th(scope='col') Steam Id
                            tbody
                            each register, i in registers
                                tr
                                th(scope='row')= register.id
                                td= register.playerName
                                td= register.playerContact
                                td= register.playerUsername
                                td= register.playerSteamId

Solved it, here's how I managed to solved it.解决了,这就是我设法解决的方法。

router.get('/:id', function(req, res){
    Tournament.findById(req.params.id, function(err, tournament){
        User.findById(tournament.author, function(err, user){
            Register.find({tournamentId: req.params.id}, function(err, registers){
                Register.count({tournamentId: req.params.id}, function(err, register){
                    if(err){
                        console.log(err);
                        return;
                    }
                    else{
                        res.render('tournament',{
                            tournament: tournament,
                            author: user.username,
                            user: req.user,
                            registers: registers,
                            register: register
                        });
                    }
                });
            });
        });
    });
});
strong Participants List (Count Number of Participant: #{register})
                        br
                        br
                        br
                        table.table
                            thead
                                tr
                                th(scope='col') Register Id
                                th(scope='col') Full Name
                                th(scope='col') Contact Number
                                th(scope='col') Username
                                th(scope='col') Steam Id
                            tbody
                            each register, i in registers
                                tr
                                th(scope='row')= register.id
                                td= register.playerName
                                td= register.playerContact
                                td= register.playerUsername
                                td= register.playerSteamId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM