我有一个MongoDB收藏
Companies
这样地:
[
{
"stock": "GOOGLE",
"percentChange": 0
},
{
"stock": "FACEBOOK",
"percentChange": 0
}
]
我有一个很大的
StockPrices
代表股票及其价格的对象:
'GOOGLE': {
prices: {
marketOpenPrice: 90.05,
highestPriceInADay: 93.45,
lowestPriceInADay: 90.05,
marketClosePrice: 91.3
}
},
'FACEBOOK': {
prices: {
marketOpenPrice: 10,
highestPriceInADay: 15,
lowestPriceInADay: 17,
marketClosePrice: 12
}
}
... and more companies like this
对于MongoDB集合中的每个公司
公司
,我需要计算并设置
percentChange
使用以下公式:
profitLoss=marketOpenPrice—marketClosePrice;
百分比变化=(profitLoss/marketOpenPrice)*100
我需要更新收藏中的每一个库存
股票价格
对象使用节点.js.
我想到了以下方法:
-
迭代集合文档
-
对于每个文档,获取“stock”字段值(GOOGLE、FACEBOOK)
-
计算
percentageChange
价值来自
StockPrices.prices
wrt公司使用上述公式
-
更新文档
这是一个不可接受的方法,因为我有成千上万的记录和更新需要立即。
换句话说:
如何有效地计算结果(在这种情况下)
百分比变化
)从一个物体(在这种情况下
股票价格
)字段(本例中为
股票价格
字段),并将计算结果设置为字段的值(在本例中
百分比变化
)在MongoDB集合中(在本例中
公司
)