如何使用猫鼬,节点,在数组中找到总和 objects
How to find sum objects inside an array using mongoose ,node ,
这是我的 collection ...
var emp = new Schema({
names: String,
details: [{
date: String,
wage: String,
sack: String,
sellername: String
}]
});
collection
的可能输出
{ 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
}]
}
我需要添加现场工资的值并将其作为总计显示在车把模板中..这里我需要的是总结 objects 数组中的工资值并过滤一些条件我尝试了很多方法这里有一些
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
你可能需要 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);
});
这是我的 collection ...
var emp = new Schema({
names: String,
details: [{
date: String,
wage: String,
sack: String,
sellername: String
}]
});
collection
的可能输出{ 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
}]
}
我需要添加现场工资的值并将其作为总计显示在车把模板中..这里我需要的是总结 objects 数组中的工资值并过滤一些条件我尝试了很多方法这里有一些
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
你可能需要 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);
});