英文:
MongoDB does not save timestamps
问题
我需要在数据库中保存createdAt
和updatedAt
属性的时间戳,但它们并没有自动保存,而是保存为{T: 0, I: 0}
。我正在使用Mongo Driver执行CRUD操作。
因此,在将当前时间附加到用户模型时,我遇到了另一个问题;我在某个地方看到,createdAt
和updatedAt
都必须是primitive.Timestamp
类型,但我不知道如何真正保存它们。
我尝试了以下赋值给User.CreatedAt
的方式:
time.Now()
new primitive.Timestamp(time.Now(), 1))
primitive.Timestamp{ T: uint32(time.Now().Unix()), I: 0 }
(这似乎有效)
回到主要问题,最好的解决方案应该是数据库允许我在触发insertOne()
时自动保存时间戳。
只要将primitive.Timestamp
赋值给User.CreatedAt
就可以了。
这是我的模型:
package models
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type User struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Email string `bson:"email" json:"email"`
CreatedAt primitive.Timestamp `bson:"createdAt" json:"createdAt"`
UpdatedAt primitive.Timestamp `bson:"updatedAt" json:"updatedAt"`
}
这是我的服务:
func CreateUser(user models.User) (models.User, error) {
db := database.GetDatabase()
result, err := db.Collection(collection).InsertOne(context.TODO(), user)
if err != nil {
return models.User{}, err
}
user.Id = result.InsertedID.(primitive.ObjectID)
user.CreatedAt = primitive.Timestamp{ T: uint32(time.Now().Unix()), I: 0 }
return user, nil
}
所以,像这样管理时间戳是可以的,还是我搞错了?
英文:
I need to save the timestamps for createdAt
and updatedAt
properties in the database, but they are not being saved automatically, they are being saved as {T: 0, I: 0}
. I am using Mongo Driver to perform CRUD operations.
So, this leds me to another problem while attaching the current time to the user model; I read somewhere that both createdAt
and updatedAt
have to be primitive.Timestamp
, but I don't know how to really save them.
I have tried User.CreatedAt =
with:
time.Now()
new primitive.Timestamp(time.Now(), 1))
primitive.Timestamp{ T: uint32(time.Now().Unix()), I: 0 }
(this seems to be working)
Getting back to the main problem, the best solution should be that the database allows me to configure the timestamps to be saved automatically whenever a insertOne()
is triggered.
That could work always that assigning primitive.Timestamp
to User.CreatedAt
is correct.
So, this is my model:
package models
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type User struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Email string `bson:"email" json:"email"`
CreatedAt primitive.Timestamp `bson:"createdAt" json:"createdAt"`
UpdatedAt primitive.Timestamp `bson:"updatedAt" json:"updatedAt"`
}
And this is my service:
func CreateUser(user models.User) (models.User, error) {
db := database.GetDatabase()
result, err := db.Collection(collection).InsertOne(context.TODO(), user)
if err != nil {
return models.User{}, err
}
user.Id = result.InsertedID.(primitive.ObjectID)
user.CreatedAt = primitive.Timestamp{ T: uint32(time.Now().Unix()), I: 0 }
return user, nil
}
So, it is ok to manage the timestamps like this or did I just mistaken?
答案1
得分: 1
你可以简单地使用time.Time
:
type User struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Email string `bson:"email" json:"email"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
MongoDB不会自动更新这些字段。在将它们存储到数据库之前,你需要自己设置它们:
user.CreatedAt = time.Now()
user.UpdatedAt = user.CreatedAt
result, err := db.Collection(collection).InsertOne(context.TODO(), user)
英文:
You can simply use time.Time
:
type User struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Email string `bson:"email" json:"email"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
MongoDB does not update these for you. You have to set them yourself before storing them in the DB:
user.CreatedAt=time.Now()
user.UpdatedAd=user.CreatedAt
result, err := db.Collection(collection).InsertOne(context.TODO(), user)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论