Mongodb Timeseries / Golang – [‘timestamp’必须存在并包含有效的BSON UTC日期时间值]

huangapple go评论115阅读模式
英文:

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:

[&#39;timestamp&#39; must be present and contain a valid BSON UTC datetime value]

code:

func CreateDevicesReadings(c *gin.Context) {

var devicesReadings DevicesReadings
c.BindJSON(&amp;devicesReadings)

// Connect to MongoDB
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
	c.JSON(500, gin.H{
		&quot;message&quot;: &quot;Internal Server Error. Could not connect to the database.&quot;,

	})
	log.Default().Println(err)
}

collection := client.Database(&quot;florly&quot;).Collection(&quot;devicesReadings&quot;)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)


// Set timestamp to the current time at the moment of the request
for i := 0; i &lt; len(devicesReadings.DevicesReadings); i++ {
	devicesReadings.DevicesReadings[i].Timestamp = time.Now().UTC()
} 
_, err = collection.InsertOne(ctx, devicesReadings)
if err != nil {
	c.JSON(500, gin.H{
		&quot;message&quot;: &quot;Internal Server Error. Could not insert the data into the database.&quot;,
	})
	log.Default().Println(err)
} else {
	log.Default().Println(&quot;Data inserted successfully.&quot;)
}

client.Disconnect(context.Background())
}

type DeviceReadings struct {
	ID 		primitive.ObjectID `json:&quot;_id,omitempty&quot; bson:&quot;_id,omitempty&quot;`
	Alias          string `json:&quot;alias&quot; bson:&quot;alias&quot;`
	Timestamp 	time.Time `json:&quot;timestamp,omitempty&quot; bson:&quot;timestamp&quot;`
	SystemReadings SystemReadings `json:&quot;systemReadings&quot; bson:&quot;systemReadings&quot;`
	SensorReadings SensorReadings `json:&quot;sensorsReadings&quot; bson:&quot;sensorsReadings&quot;`
}

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.

huangapple
  • 本文由 发表于 2023年2月26日 23:15:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定