Go Mongox轻松实现MongoDB的时间字段自动填充

2025-02-12 05:50

本文主要是介绍Go Mongox轻松实现MongoDB的时间字段自动填充,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以...

前言

MongoDB 的集合中,时间字段(如 创建时间更新时间)通常是必不可少的。在使用 Go 语言操作 MongoDB 时,例如执行插入或更新操作,我们需要手动设置这些时间字段的值。然而,每次手动赋值不仅繁琐,还容易导致代码重复。那么,是否可以在程序层面实现自动填充呢?目前,官方的 mongo-go-driver 并不支持自动填充时间字段,而 mongox 库提供了这一能力。本文将介绍如何使用 mongox 库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码。

Go Mongox轻松实现MongoDB的时间字段自动填充

时间字段填充规则

在定义结构体时,如果字段符合以下特性,则可以被自动填充:

字段名称和类型符合规定

结构体字段名为 CreatedAtUpdatedAt 字段,且类型为 time.Timeint/int64。当为 int/int64 时,将会填充当前时间戳秒数。

字段包含特定标签

  • mongox:"autoCreateTime":在插入文档时,如果该字段的值为零值,则会自动设置为当前时间。除了 time.Time 类型,你还可以使用 secondmillinano 三种时间戳精度,使用样例:mongox:"autoCreateTime:milli" 如果不指定 milli,默认是 second
  • mongox:"autoUpdateTime":在插入文档时,如果该字段的值为零值或更新文档时,会自动设置为当前时间。除了 time.Time 类型,你还可以使用 secondmillinano 三种时间戳精度。使用样例:mongox:"autoUpdateTime:milli" 如果不指定 milli,默认是 second

Mongox 的安装

通过以下命令安装 mongox 库:

go get github.com/chenmingyong0423/go-mongox/v2

使用 Mongox 进行插入操作

结构体定义

type User struct {
    ID        bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
    Name      string        `bson:"name"`
    Age       int           `bson:"age"`
    CreatedAt time.Time     `bson:"created_at"`
    UpdatedAt int           `bson:"updated_at"` // 使用秒级时间戳填充字段

    CreateSecondTime int64 `bson:"create_second_time" mongox:"autoCreateTime"`        // 使用秒级时间戳填充字段
    UpdateSecondTime int64 `bson:"update_second_time" mongox:"autoUpdateTime:second"` // 使用秒级时间戳填充字段
    CreateMilliTime  int64 `bson:"create_milli_time" mongox:"autoCreateTime:milli"`   // 使用毫秒级时间戳填充字段
    UpdateMilliTime  int64 `bson:"update_milli_time" mongox:"autoUpdateTime:milli"`   // 使用毫秒级时间戳填充字段
    CreateNanoTime   int64 `bson:"create_nano_time" mongox:"autoCreateTime:nano"`     // 使用纳秒级时间戳填充字段
    UpdateNanoTime   int64 `bson:"update_nano_time" mongox:"autoUpdateTime:nano"`     // 使用纳秒级时间戳填充字段
}

示例代码

package main

import (
    "context"
    "fmt"
    "time"

    "go.mongodb.org/mongo-driver/v2/bson"

    "go.mongodb.org/mongo-driver/v2/mongo"
    "go.mongodb.org/mongo-driver/v2/mongo/options"
    "go.mongodb.org/mongo-driver/v2/mongo/readpref"

    "github.com/chenmingyong0423/go-mongox/v2"
)

type User struct {
    ID        bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
    Name      string        `bson:"name"`
    Age       int           `bson:"age"`
    CreatedAt time.Time     `bson:"created_at"`
    UpdatedAt int           `bson:"updated_at"` // 使用秒级时间戳填充字段

    CreateSecondTime int64 `bson:"create_second_time" mongox:"autoCreateTime"`        // 使用秒级时间戳填充字段
    UpdateSecondTime int64 `bson:"update_second_time" mongox:"autoUpdateTime:second"` // 使用秒级时间戳填充字段
    CreateMilliTime  int64 `bson:"create_milli_time" mongox:"autoCreateTime:milli"`   // 使用毫秒级时间戳填充字段
    UpdateMilliTime  int64 `bson:"update_milli_time" mongox:"autoUpdateTime:milli"`   // 使用毫秒级时间戳填充字段
    CreateNanoTime   int64 `bson:"create_nano_time" mongox:"autoCreateTime:nano"`     // 使用纳秒级时间戳填充字段
    UpdateNanoTime   int64 `bson:"update_nano_time" mongox:"autoUpdateTime:nano"`     // 使用纳秒级时间戳填充字段
}

func main() {
    mongoClient, err := newMongoClient()
    if err != nil {
        panic(err)
    }
    client := mongox.NewClient(mongoClient, &mongox.Config{})
    database := client.NewDatabase("db-test")

    userColl := mongox.NewCollection[User](database, "users")

    user := &User{
        Name: "陈明勇",
        Age:  18,
    }
    _, err = userColl.Creator().InsertOne(context.Background(), user)
    if err != nil {
        panic(err)
    }
    fmt.Println(!user.CreatedAt.IsZero())   // true
    fmt.Println(user.UpdatedAt != 0)        // true
    fmt.Println(user.CreateSecondTime != 0) // true
    fmt.Println(user.UpdateSecondTime != 0) // true
    fmt.Println(user.CreateMilliTime != 0)  // true
    fmt.Println(user.UpdateMilliTime != 0)  // true
    fmt.Println(user.CreateNanoTime != 0)   // true
    fmt.Println(user.UpdateNanoTime != 0)   // true
}

// 示例代码,仅供参考
func newMongoClient() (*mongo.Client, error) {
    client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
        Username:   "test",
        Password:   "test",
        AuthSource: "db-test",
    }))
    if err != nil {
        return nil, err
    }
    err = client.Ping(context.Background(), readpref.Primary())
    if err != nil {
        panic(err)
    }
    return client, nil
}

插入数据后,通过零值比较判断字段值是否被填充。fmt.Println 语句都输出 true,说明所有时间字段的值都被填充。

使用 Mongox 进行更新操作

更新操作

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/chenmingyong0423/go-mongox/v2/builder/query"
    "github.com/chenmingyong0423/go-mongox/v2/builder/update"

    "go.mongodb.org/mongo-driver/v2/bson"

    "go.mongodb.org/mongo-driver/v2/mongo"
    "go.mongodb.org/mongo-driver/v2/mongo/options"
    "go.mongodb.org/mongo-driver/v2/mongo/readpref"

    "github.com/chenmingyong0423/go-mongox/v2"
)

type User struct {
    ID        bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
    Name      string        `bson:"name"`
    Age       int           `bson:"age"`
    CreatedAt time.Time     `bson:"created_at"`
    UpdatedAt int           `bson:"updated_at"` // 使用秒级时间戳填充字段

    CreateSecondTime int64 `bson:"create_second_time" mongox:"autoCreateTime"`        // 使用秒级时间戳填充字段
    UpdateSecondTime int64 `bson:"update_second_time" mongox:"autoUpdateTime:second"` // 使用秒级时间戳填充字段
    CreateMilliTime  int64 `bson:"create_milli_time" mongox:"autoCreateTime:milli"`   // 使用毫秒级时间戳填充字段
    UpdateMilliTime  int64 `bson:"update_milli_time" mongox:"autoUpdateTime:milli"`   // 使用毫秒级时间戳填充字段
    CreateNanoTime   int64 `bson:"create_nano_time" mongox:"autoCreateTime:nano"`     // 使用纳秒级时间戳填充字段
    UpdateNanoTime   int64 `bson:"update_nano_time" mongox:"autoUpdateTime:nano"`     // 使用纳秒级时间戳填充字段
}

func main() {
    mongoClient, err := newMongoClient()
    if err != nil {
        panic(err)
    }
    client := mongox.NewClient(mongoClient, &mongox.Config{})
    database := client.NewDatabase("db-test")

    userColl := mongox.NewCollection[User](database, "users")

    // 用于比较后面的时间字段是否更新
    now := time.Now()

    _, err = userColl.Updater().
        Filter(query.Eq("name", "陈明勇")).
        Updates(update.Set("age", 26)).
        UpdateOne(context.Background())
    if err != nil {
        panic(err)
    }

    user, err := userColl.Finder().
        Filter(query.Eq("name", "陈明勇")).
        FindOne(context.Background())
    if err != nil {
        panic(err)
    }

    fmt.Println(user.UpdatedAt > int(now.Unix()))   // true
    fmt.Println(user.UpdateSecondTime > now.Unix()) // true
    fmt.Println(user.UpdateMilliTime > now.Unix())  // true
    fmt.Println(user.UpdateNanoTime > now.Unix())   // true
}

// 示例代码,仅供参考
func newMongoClient() (*mongo.Client, error) {
    client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
        Username:   "test",
        Password:   "test",
        AuthSource: "db-test",
    }))
    if err != nil {
        return nil, err
    }
    err = client.Ping(context.Background(), readpref.Primary())
    if err != nil {
        panic(err)
    }
    return client, nil
}

updates 参数无需指定时间字段,也能自动填充。更新数据后,通过与 now 进行比较判断字段值是否被填充。fmt.Println 语句都输出 true,说明更新时间字段的值都已更新。

Upsert 操作

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/chenmingyong0423/go-mongox/v2/builder/query"
    "github.com/chenmingyong0423/go-mongox/v2/builder/update"

    "go.mongodb.org/mongo-driver/v2/bson"

    "go.mongodb.org/mongo-driver/v2/mongo"
    "go.mongodb.org/mongo-driver/v2/mongo/options"
    "go.mongodb.org/mongo-driver/v2/mongo/readpref"

    "github.com/chenmingyong0423/go-mongox/v2"
)

type User struct {
    ID        bson.ObjectID `bson:"_id,omitempty" mongoxpython:"autoID"`
    Name      string        `bson:"name"`
    Age       int           `bson:"age"`
    CreatedAt time.Time     `bson:"created_at"`
    UpdatedAt int           `bson:"updated_at"` // 使用秒级时间戳填充字段

    CreateSecondTime int64 `bson:"create_second_time" mongox:"autoCreateTime"`        // 使用秒级时间戳填充字段
    UpdateSecondTime int64 `bson:"update_second_time" mongox:"autoUpdateTime:second"` // 使用秒级时间戳填充字段
    CreateMilliTime  int64 `bson:"create_milli_time" mongox:"autoCreateTime:milli"`   // 使用毫秒级时间戳填充字段
    UpdateMilliTime  int64 `bson:"update_milli_time" mongox:"autoUpdateTime:milli"`   // 使用毫秒级时间戳填充字段
 php   CreateNanoTime   int64 `bson:"create_nano_time" mongox:"autoCreateTime:nano"`     // 使用纳秒级时间戳填充字段
    UpdateNanoTime   int64 `bson:"update_nano_time" mongox:"autoUpdateTime:nano"`     // 使用纳秒级时间戳填充字段
}

func main() {
    mongoClient, err := newMongoClient()
    if err != nil {
        panic(err)
    }
    client := mongox.NewClient(mongoClient, &mongox.Config{})
    database := client.NewDatabase("db-test")

    userColl := mongox.NewCollection[User](database, "users")

    _, err = userColl.Updater().
        Filter(query.Eq("name", "Mingyong Chen")).
        Updates(update.Set("age", 18)).
        Upsert(context.Background())
    if err != nil {
 android       panic(err)
    }

    user, err := userColl.Finder().
        Filter(query.Eq("name", "Mingyong Chen")).
        FindOne(context.Background())
    if err != nil {
        panic(err)
    }

    fmt.Println(!user.CreatedAt.IsZero())   // true
    fmt.Println(user.UpdatedAt != 0)        // true
    fmt.Println(user.CreateSecondTime != 0) // true
    fmt.Println(user.UpdateSecondTime != 0) // true
    fmt.Println(user.CreateMilliTime != 0)  // true
    fmt.Println(user.UpdateMilliTime != 0)  // true
    fmt.Println(user.CreateNanoTime != 0)  js // true
    fmt.Println(user.UpdateNanoTime != 0)   // true
}

// 示例代码,仅供参考
func newMongoClient() (*mongo.Client, error) {
    client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(optionwww.chinasem.cns.Credential{
        Username:   "test",
        Password:   "test",
        AuthSource: "db-test",
    }))
    if err != nil {
        return nil, err
    }
    err = client.Ping(context.Background(), readpref.Primary())
    if err != nil {
        panic(err)
    }
    return client, nil
}

当触发 Upsert 操作时,无需指定字段,创建时间和更新时间字段都会被填充。fmt.Println 语句都输出 true,说明所有时间字段的值都被填充。

小结

本文详细介绍了如何使用 mongox 库,在插入和更新数据时自动填充时间字段。在定义结构体时,只要满足 字段名称和类型符合规定字段包含特定标签mongox 将会自动填充时间字段的值。

到此这篇关于Go Mongox轻松实现MongoDB的时间字段自动填充的文章就介绍到这了,更多相关Go Mongox使用内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!

这篇关于Go Mongox轻松实现MongoDB的时间字段自动填充的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1153410

相关文章

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

一文教你使用Python实现本地分页

《一文教你使用Python实现本地分页》这篇文章主要为大家详细介绍了Python如何实现本地分页的算法,主要针对二级数据结构,文中的示例代码简洁易懂,有需要的小伙伴可以了解下... 在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放

SpringMVC前后端传值的几种实现方式

《SpringMVC前后端传值的几种实现方式》本文主要介绍了SpringMVC前后端传值的方式实现,包括使用HttpServletRequest、HttpSession、Model和ModelAndV... 目录一、从Controller层到JSP界面1、使用HttpServletRequest的方式2、使

树莓派启动python的实现方法

《树莓派启动python的实现方法》本文主要介绍了树莓派启动python的实现方法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录一、RASPBerry系统设置二、使用sandroidsh连接上开发板Raspberry Pi三、运

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表