.project({
percentage_amount: {
$multiply: ["$commission", {
$divide: ["$price", 100]
}]
},
payable: {
$subtract: ["$price", percentage_amount]
}
})
这在 Mongoose 中可能吗?如果没有,该怎么做?请帮助我
最佳答案
是的,使用聚合框架是可能的。您可以使用aggregate()
管道构建器为:
Model.aggregate()
.project({
"percentage_amount": {
"$multiply": [
"$commission", {
"$divide": ["$price", 100]
}
]
},
"payable": {
"$subtract": ["$price", percentage_amount]
}
})
.exec(function(err, result) {
if (err) return handleError(err);
console.log(result)
});
或使用aggregate()
运算符管道数组:
Model.aggregate([
{
"project": {
"percentage_amount": {
"$multiply": [
"$commission", {
"$divide": ["$price", 100]
}
]
},
"payable": {
"$subtract": ["$price", percentage_amount]
}
}
]).exec(function(err, result) {
if (err) return handleError(err);
console.log(result)
});
关于node.js - 在 mongoose 的计算中使用项目字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138991/