我尝试制作一个存储Fitbit数据的应用,每天使用一个子网。这是我的架构:
var fitbitTrackerDailySchema = mongoose.Schema({
dateTime : Date,
value : Number
});
var userSchema = mongoose.Schema({
email : String,
password : String,
fitbit : {
displayName : String,
id : String,
accessToken : String,
refreshToken : String,
trackerSteps : [fitbitTrackerDailySchema]
});
当我从fitbit的API接收新数据时,我想查找具有相同日期的子文档并更新它,或者如果那天没有数据则添加新的子文档。例如: 存储在我的数据库中的数据:
{
"_id" : ObjectId("5774df5f2d9014bdc0443706"),
"email" : "drp@test.com",
"password" : "mypassword",
"fitbit" : {
"id" : "12345",
"displayName" : "myname",
"accessToken" : "sometoken",
"refreshToken" : "anothertoken"
"trackerSteps" : [
{
"value" : 12538,
"dateTime" : ISODate("2016-06-24T00:00:00.000Z")
}]
}
}
我收到了来自fitbit的更新:
{
"activities-log-steps":[
{"dateTime":"2011-06-24","value":15490},
{"dateTime":"2011-06-25","value":10344}]
}
我的数据库应该是:
{
"_id" : ObjectId("5774df5f2d9014bdc0443706"),
"email" : "drp@test.com",
"password" : "mypassword",
"fitbit" : {
"id" : "12345",
"displayName" : "myname",
"accessToken" : "sometoken",
"refreshToken" : "anothertoken"
"trackerSteps" : [
{
"value" : 15490, /*value updated*/
"dateTime" : ISODate("2016-06-24T00:00:00.000Z")
},
{
"value" : 10344, /*new day*/
"dateTime" : ISODate("2016-06-25T00:00:00.000Z")
}]
}
}
我应该怎么做?有什么想法吗?