在保存数据的形式时,数据是由ajax请求发送给golang api的,但我正在检索的数据却是:
[
{
"day":"Monday",
"time_slug":13,
"start_time":3600,
"end_time":7200,
"count":2
},
{"day":"Monday",
"time_slug":15,
"start_time":7200,
"end_time":10800,
"count":3
},
{"day":"Monday",
"time_slug":11,
"start_time":14400,
"end_time":18000,
"count":4
},
{"day":"Monday",
"time_slug":7,
"start_time":21600,
"end_time":25200,
"count":5
}
]
这是ajax发送到go api的数据,我太成功地检索了这些数据,并将这些值也保存在mongodb集合中servent_count
。看到有一个计数字段"count" = integer_value
据此,我想将时间计数的数量存储在不同的不同集合中。示例假设数据将是:
[
{
"day":"Monday",
"time_slug":13,
"start_time":3600,
"end_time":7200,
"count":2
},
{"day":"Monday",
"time_slug":15,
"start_time":7200,
"end_time":10800,
"count":3
}
]
在上述数据中,有两个数据的第一个count
值为2
,另一个为3
,我只想将第一个计数值保存到集合available_counts
中左侧计数值和该数据一起存储在additional_counts
中,还有一个状态字段,该状态字段告诉我们是否已预订了仆人,如果已预订,则它将从additional_counts
中获取该值,然后在additional_counts
不断更新,计数值将减少1。
为此使用的代码I,m是:-
var providerspot models.ProviderSpot
func SaveProviderSpot(c *gin.Context) {
response := ResponseController{}
values := c.PostForm("array")
fmt.Println(values)
var err error
byt := []byte(values)
var result models.ProviderSpots
if err = json.Unmarshal(byt, &result); err != nil{
fmt.Println(err)
}
fmt.Println(result)
for i := 0; i < len(result); i++ {
providerspot.Day = result[i].Day
providerspot.TimeSlug = result[i].TimeSlug
providerspot.StartTime = result[i].StartTime
providerspot.EndTime = result[i].EndTime
providerspot.Count = result[i].Count
id, _ := models.GetAutoIncrementCounter(config.ProvidersSpotsCounterId, config.ProvidersSpotsCollection)
providerspot.Id = id
err = models.AddProviderSpot(&providerspot)
}
if err != nil {
errMsg := "Error while saving the provider spot Please try again."
response = ResponseController{
config.FailureCode,
config.FailureFlag,
errMsg,
nil,
}
} else {
successMsg := "The provider spot has been saved successfully!"
response = ResponseController{
config.SuccessCode,
config.SuccessFlag,
successMsg,
nil,
}
GetResponse(c, response)
return
}
}
type ProviderSpot struct {
Id int `json:"_id" bson:"_id"`
Day string `json:"day" bson:"day"`
TimeSlug int `json:"time_slug" bson:"time_slug"`
StartTime int64 `json:"start_time" bson:"start_time"`
EndTime int64 `json:"end_time" bson:"end_time"`
Count int `json:"count" bson:"count"`
}
type ProviderSpots []ProviderSpot
任何人都可以告诉我该怎么做。