小编caf*_*ech的帖子

MongoDB 数组中对象的聚合总和

我已经看到了很多关于如何对数组中的数组中的对象的属性求和的答案,但我正在尝试对跨文档的数组中的对象的各个属性求和。例如,给定此文档结构:

{
   "id": 1,
   "stats": [
     {
       "number": 100,
       "year": 2014
     },
     {
       "number": 200,
       "year": 2015
     }
]
},


{
   "id": 2,
   "stats": [
     {
       "number": 50,
       "year": 2014
     },
     {
       "number": 75,
       "year": 2015
     }
]
}
Run Code Online (Sandbox Code Playgroud)

期望的输出是:

{
   "stats": [
     {
       "number": 150,
       "year": 2014
     },
     {
       "number": 275,
       "year": 2015
     }
}
Run Code Online (Sandbox Code Playgroud)

我不想对number2014 年和 2015 年的财产进行求和,我想对 2014 年的两个文档进行求和。

mongodb aggregation-framework

5
推荐指数
1
解决办法
7761
查看次数

对渲染进行反应以显示先前的状态值

我是ReactJS的初学者,刚开始学习并开始编写用于猜测数字的代码,但是猜测计数显示不同的值。{this.state.attempts}持有编号 用户尝试查找答案以显示正确值的尝试次数。但是{this.state.result}显示每次点击的结果,但是如果用户找到答案,它将显示以前的状态。我想知道这是怎么发生的。那是因为它不在下面render()吗?

import React, { Component } from 'react';

export default class NoLifeCycComps extends Component {
    constructor(props) {
        super(props);
        this.state = this.getInitialState();
        this.checkValue = this.checkValue.bind(this);
        this.updateInput = this.updateInput.bind(this);
    }

    randNum(){
        return Math.floor((Math.random() * 100) + 1);
    }

    getInitialState(){
        return {
            num: this.randNum(),
            inputValue: '',
            attempts: 0,
            result: '',
            reset : false
        }
    }

    reset() {
        this.setState(this.getInitialState());
    }

    checkValue() {
        this.setState((prevState) => {
            return { attempts: prevState.attempts + 1 }
        });
        if (this.state.inputValue > this.state.num) …
Run Code Online (Sandbox Code Playgroud)

render reactjs

3
推荐指数
1
解决办法
1917
查看次数

标签 统计

aggregation-framework ×1

mongodb ×1

reactjs ×1

render ×1