我有一个集合,其中一个字段带有数字,并且在该集合中填充了ObjectId,其中还有一个带有数字的字段。看起来像这样:
{" _id":" 5abe65e298002b2334bb1470"" electoralUnit":" 5ab906612f30fe23dc592591"" turnoutByHour":& #34; 08:00"" voterTurnout":10," safeVoter":5," __ v":0}
{" _id":" 5ab906612f30fe23dc592591""镇":" 5ab903952e9dc70408a81e32""名称":& #34;东西"" __ v":0," electoralNumber":4200," safeVoter" 360}
我的路线如下:
router.get('/', (req, res) => {
VoterTurnout.find({})
.sort({turnoutByHour: 1})
.populate('electoralUnit')
.then(voterTurnouts => {
ElectoralUnit.find({})
.then(electoralUnits => {
res.render('home/voterTurnouts/index', {
voterTurnouts: voterTurnouts,
electoralUnits: electoralUnits
});
});
});
});
查看:
<tbody>
{{#each voterTurnouts}}
<tr data-row="{{electoralUnit.id}}">
<th scope="row">{{turnoutByHour}}</th>
<td>{{voterTurnout}} ({{Math.floor(voterTurnout * 100 / electoralUnit.electoralNumber)}}%)</td> // This doesn't working
<td>{{safeVoter}}</td>
<td><a href="/voterTurnouts/edit/{{id}}" class="btn btn-outline-info btn-sm">Izmeni</a></td>
</tr>
{{/each}}
</tbody>
我读了一些关于mongodb聚合的内容,但是找不到一些可以帮助我解决这个问题的例子...有什么帮助吗?
答案 0 :(得分:0)
我用把手定制助手完成了它。我google this把手自定义助手并稍加编辑:
math: function(lvalue, operator, rvalue, options) {
lvalue = parseFloat(lvalue);
rvalue = parseFloat(rvalue);
return {
"+": lvalue + rvalue,
"-": lvalue - rvalue,
"*": lvalue * rvalue,
"/": lvalue / rvalue,
"%": Math.floor((lvalue / rvalue) * 100)
}[operator];
}
然后导出数学值以查看并添加此把手:
{{math voterTurnout&#34;%&#34; electoralUnit.electoralNumber}}%
它正在工作。