簡體   English   中英

如何使用 mongoose,node, 在數組中查找 sum 對象

[英]How to find sum objects inside an array using mongoose ,node ,

這是我的收藏...

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

});

可能的集合的 output

{   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> 

似乎因為details是一個數組,您不能只通過"$details.wage"來定位每個項目的wage ,您可能需要展開(參見示例用法)該數組將生成多個條目,其中包含details object可以使用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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM