英文:
Mongodb Timeseries / Golang - ['timestamp' must be present and contain a valid BSON UTC datetime value]
问题
我有以下示例Go代码,用于将来自REST请求(GIN)的数据插入到MongoDB中,但是出现了错误:
['timestamp'必须存在并包含有效的BSON UTC日期时间值]
代码:
func CreateDevicesReadings(c *gin.Context) {
var devicesReadings DevicesReadings
c.BindJSON(&devicesReadings)
// 连接到MongoDB
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
c.JSON(500, gin.H{
"message": "内部服务器错误。无法连接到数据库。",
})
log.Default().Println(err)
}
collection := client.Database("florly").Collection("devicesReadings")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
// 将时间戳设置为请求时的当前时间
for i := 0; i < len(devicesReadings.DevicesReadings); i++ {
devicesReadings.DevicesReadings[i].Timestamp = time.Now().UTC()
}
_, err = collection.InsertOne(ctx, devicesReadings)
if err != nil {
c.JSON(500, gin.H{
"message": "内部服务器错误。无法将数据插入数据库。",
})
log.Default().Println(err)
} else {
log.Default().Println("数据插入成功。")
}
client.Disconnect(context.Background())
}
type DeviceReadings struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Alias string `json:"alias" bson:"alias"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp"`
SystemReadings SystemReadings `json:"systemReadings" bson:"systemReadings"`
SensorReadings SensorReadings `json:"sensorsReadings" bson:"sensorsReadings"`
}
我做错了什么?我以为MongoDB会完成将time.Time
类型转换为MongoDB所需类型的整个过程。
英文:
I have the following example Go code that inserts data into MongoDB coming from a REST request (GIN), but it fails with:
['timestamp' must be present and contain a valid BSON UTC datetime value]
code:
func CreateDevicesReadings(c *gin.Context) {
var devicesReadings DevicesReadings
c.BindJSON(&devicesReadings)
// Connect to MongoDB
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
c.JSON(500, gin.H{
"message": "Internal Server Error. Could not connect to the database.",
})
log.Default().Println(err)
}
collection := client.Database("florly").Collection("devicesReadings")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
// Set timestamp to the current time at the moment of the request
for i := 0; i < len(devicesReadings.DevicesReadings); i++ {
devicesReadings.DevicesReadings[i].Timestamp = time.Now().UTC()
}
_, err = collection.InsertOne(ctx, devicesReadings)
if err != nil {
c.JSON(500, gin.H{
"message": "Internal Server Error. Could not insert the data into the database.",
})
log.Default().Println(err)
} else {
log.Default().Println("Data inserted successfully.")
}
client.Disconnect(context.Background())
}
type DeviceReadings struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Alias string `json:"alias" bson:"alias"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp"`
SystemReadings SystemReadings `json:"systemReadings" bson:"systemReadings"`
SensorReadings SensorReadings `json:"sensorsReadings" bson:"sensorsReadings"`
}
What I'm doing wrong? I thought that Mongodb does the whole process of converting type time.time
to the type MongoDB looks for.
答案1
得分: 0
你调用了Collection.InsertOne()
,它用于插入单个文档。然而,devicesReadings
是一个包含多个文档的切片。
所以你要么遍历所有文档并将它们逐个传递给Collection.InsertOne()
,要么使用Collection.InsertMany()
,使用包含要插入的多个文档的切片。
英文:
You call Collection.InsertOne()
, which can be used to insert a single document. Yet, devicesReadings
is a slice of multiple documents.
So you either have to iterate over all documents and pass those individually to Collection.InsertOne()
, or use Collection.InsertMany()
, using a slice of multiple documents you want to insert.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论