Collective cooperation is essential to human society, and it exists in many social dilemmas. In the scenario of a collective-risk social dilemma, a group of players have to collectively contribute to a public fund to prevent the tragedy of the commons, such as dangerous climate change, because everybody will lose all their remaining money when the damage happens with a certain probability if the group fails to reach a fixed fundraising target. Yet, it remains largely unclear how the group size affects the probability of reaching the collective target and the mechanism that drives different outcomes of the collective cooperation. Here, we contribute to the literature by exploring the role of group size in the collective-risk social dilemma and the potential underlying mechanism using both model simulations and human experiments. Through simulations we found that the rate of failure for collective cooperation increases for larger groups, along with the arising of bystander effect and a decrease in average contributions, which are confirmed by our experimental observations. We further analyze the patterns of investment behaviors in the experiment setting by categorizing players into cooperators, altruists, and free riders using both a clustering method and a golden standard. We found that altruists who tend to contribute more, rather than cooperators who prefer contributing a fair-share investment, play a crucial role in groups with success outcome in early and/or middle stages of the game. Our results indicate that bystanders are dynamic and their amount depends on the contribution of others. When others contribute less, bystanders also contribute less. If the collective goal is unlikely to achieve, more players choose to be bystanders who strategically contribute less, intriguing the failure of the collective goal. Our findings suggest a potentially effective way to solve the collective-risk social dilemma by reducing the bystander effect through the mechanism design of forming small groups.