MongoDB不保存时间戳。

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

MongoDB does not save timestamps

问题

我需要在数据库中保存createdAtupdatedAt属性的时间戳,但它们并没有自动保存,而是保存为{T: 0, I: 0}。我正在使用Mongo Driver执行CRUD操作。

因此,在将当前时间附加到用户模型时,我遇到了另一个问题;我在某个地方看到,createdAtupdatedAt都必须是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)

huangapple
  • 本文由 发表于 2021年12月1日 10:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70178344.html
匿名

发表评论

匿名网友

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

确定