【问题标题】:How to find sum objects inside an array using mongoose ,node ,如何使用 mongoose、node、在数组中查找 sum 对象
【发布时间】:2021-01-20 05:26:49
【问题描述】:

这是我的收藏...

var emp = new Schema({
    names: String,
    details: [{
        date: String,
        wage: String,
        sack: String,
        sellername: String
    }]

});

集合的可能输出

{   name: john
    details: [{
        date:12-01-2019
        wage:210
        sack:10
        sellername: cristy   
    }]
    details: [{
        date:12-01-2019
        wage:210
        sack:10
        sellername: cristy
    }]
    details: [{
        date:12-01-2019
        wage:210
        sack:10
        sellername: cristy       
    }]
}

我需要添加字段工资的值并将其显示在车把模板中作为总计..这里我需要的是通过过滤一些标准来总结对象数组中的工资值我在这里尝试了很多方法有一些

Person.aggregate([{
        "$match": {
            "name": req.body.name
        }
    },
    {
        "$addFields": {
            "total": {
                "$sum": "$details.wage"
            }
        }
    }
]).exec((err, data) => {
    if (err) console.log(err);
    console.log(data);
});

我的工作代码来显示值

Person.find({
        names: req.body.woker
    }) // <=> wrapper for Model.find() ...
    .then(documents => {
        // create context Object with 'usersDocuments' key
        const context = {
            usersDocuments: documents.map(documents => {
                return {
                    details: documents.details,
                }
            })
        }
        console.log(context.usersDocuments)

        // rendering usersDocuments from context Object
        res.render('employee/individuallist', {
            employeeName: req.body.woker,
            usersDocuments: context.usersDocuments,
        })
    })
    .catch(error => res.status(500).send(error))
})

我的车把模板代码

<table class="table table-striped" id="list">
    <thead>
        <tr>
            <th>Date</th>
            <th>Work of (Seller Name)</th>
            <th>Number of sacks</th>
            <th>Kooli</th>
        </tr>
    </thead>
    <tbody>
            
            {{#each usersDocuments}}
            {{#each this.details}}
                                                    
        <tr>
            <td>{{this.date}}</td>
            <td>{{this.sellername}}</td>
            <td>{{this.chack}}</td>
            <td>{{this.kooli}}</td>
        </tr>
                                                  
        {{/each}}

        {{/each}}
                    
        <tr>
            <td colspan="3">Total Amount</td>
            <td>{{this.total}}</td>
        </tr>
    </tbody>
</table> 

【问题讨论】:

    标签: node.js arrays mongodb mongoose handlebars.js


    【解决方案1】:

    似乎因为details 是一个数组,您不能只通过"$details.wage" 定位每个项目的wage,您可能需要unwind (see the example usage) 该数组将生成多个条目,这些条目将具有details 对象你可以在哪里使用details.wage

    也许更简单的解决方案是使用reduce 来汇总总数:

    Person.aggregate([{
        "$match": {
          "name": req.body.name
        }
      },
      {
        "$addFields": {
          "total": {
            "$reduce": {
               input: "$details",
               initialValue: 0,
               in: { 
                 $add: ["$$value", "$$this.wage"]
               }
             }
          }
        }
      }
    ]).exec((err, data) => {
        if (err) console.log(err);
        console.log(data);
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 2014-05-13
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2021-07-08
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多