我从包含年度数据(252天)的API下载了数据。每天,它将在新的一天更新值。例如(假设仅返回2天的数据),今天,API返回t,t-1。然后,tmr,API将返回t,t + 1。
如何在mongodb中更新数据库以存储t-1至t + 1数据?我试过$ addToSet和$ Push。这是示例文档:
{
"_id" : ObjectId("5bc838e7af72c90c5c2550df"),
"Ticker" : "AAPL",
"Daily" : [
{
"date" : "2017-10-17",
"open" : 157.4,
"high" : 158.4738,
"low" : 156.8582,
"close" : 158.0798,
"volume" : 18997275,
"unadjustedVolume" : 18997275,
"change" : 0.581211,
"changePercent" : 0.369,
"vwap" : 157.654,
"label" : "Oct 17, 17",
"changeOverTime" : 0
},
{
"date" : "2017-10-18",
"open" : 158.0305,
"high" : 158.3162,
"low" : 157.2227,
"close" : 157.3803,
"volume" : 16374164,
"unadjustedVolume" : 16374164,
"change" : -0.699423,
"changePercent" : -0.442,
"vwap" : 157.7015,
"label" : "Oct 18, 17",
"changeOverTime" : -0.00442498029476252
}}
但是,第二天我从API下载数据并使用update来更新文档时:
db.getCollection('HistoricalData').update(
{"Ticker":'AAPL'},
{"$addToSet":{"Daily":[{'date': '2017-10-18',
'open': 158.0305,
'high': 158.3162,
'low': 157.2227,
'close': 157.3803,
'volume': 16374164,
'unadjustedVolume': 16374164,
'change': -0.699423,
'changePercent': -0.442,
'vwap': 157.7015,
'label': 'Oct 18, 17',
'changeOverTime': -0.004424980294762521},
{'date': '2017-10-19',
'open': 154.4152,
'high': 154.7403,
'low': 152.7109,
'close': 153.6567,
'volume': 42584166,
'unadjustedVolume': 42584166,
'change': -3.7237,
'changePercent': -2.366,
'vwap': 153.4613,
'label': 'Oct 19, 17',
'changeOverTime': -0.027980172039691376}]}},
{"upsert":true}
)
它将给我一个新文档,作为元素插入到列表中。同时,我想要的是:
{
"_id" : ObjectId("5bc838e7af72c90c5c2550df"),
"Ticker" : "AAPL",
"Daily" : [
{
"date" : "2017-10-17",
"open" : 157.4,
"high" : 158.4738,
"low" : 156.8582,
"close" : 158.0798,
"volume" : 18997275,
"unadjustedVolume" : 18997275,
"change" : 0.581211,
"changePercent" : 0.369,
"vwap" : 157.654,
"label" : "Oct 17, 17",
"changeOverTime" : 0
},
{
"date" : "2017-10-18",
"open" : 158.0305,
"high" : 158.3162,
"low" : 157.2227,
"close" : 157.3803,
"volume" : 16374164,
"unadjustedVolume" : 16374164,
"change" : -0.699423,
"changePercent" : -0.442,
"vwap" : 157.7015,
"label" : "Oct 18, 17",
"changeOverTime" : -0.00442498029476252
},
{'date': '2017-10-19',
'open': 154.4152,
'high': 154.7403,
'low': 152.7109,
'close': 153.6567,
'volume': 42584166,
'unadjustedVolume': 42584166,
'change': -3.7237,
'changePercent': -2.366,
'vwap': 153.4613,
'label': 'Oct 19, 17',
'changeOverTime': -0.027980172039691376}]}
}