为什么将go struct null中的数组字段插入到mongoDB数据库中?

发布于 2025-02-09 23:57:22 字数 605 浏览 1 评论 0 原文

因此,将模型插入MongoDB时,我遇到了一些意外的行为。当我通过Postman发送一个空白到我的服务器并将其插入数据库时​​,返回的结果是postman name number 默认值为他们的预期默认值, 0 “” ,但是对于数据,而不是默认为空数组,它默认为 null ,即使尽管在Go Console插入之前和之后,其价值并非零,而是一个空数组。分配 data 字段一个空 [] int {} 在插入解决问题之前,手动将空数组作为Postman的数据字段发送,但我很好奇如果有其他方法可以保证数组字段默认为 [] 而不是 null 插入时。

这是我的模型:

type Test struct{
    Name string `json:"name"   bson:"name"`
    Number int  `json:"number" bson:"number"`
    Data []int  `json:"data"   bson:"data"`
}

So I have ran into some unexpected behavior when inserting a model into mongoDB. When I send an empty body via Postman to my server and insert it into my database, the returned result to Postman had name and number default to their expected default values, 0 and "", but for data, instead of defaulting to an empty array, it defaulted to null instead, even though its value printed out before and after insertion in the Go console isn't nil, but an empty array. Assigning the data field an empty []int{} before insertion solves the issue, and so does manually sending an empty array as the data field from Postman, but I was curious if there was any other way to guarantee that array fields default to [] and not null when getting inserted.

Here is my model:

type Test struct{
    Name string `json:"name"   bson:"name"`
    Number int  `json:"number" bson:"number"`
    Data []int  `json:"data"   bson:"data"`
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2025-02-16 23:57:22

实现bson.marshaler,当您保存值

时,将调用您的元帅()函数

package main

import (
    "log"

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

type Test struct {
    Data []int `json:"data" bson:"data"`
}

func (t *Test) MarshalBSON() ([]byte, error) {
    if t.Data == nil {
        log.Println("t.Data is nil")
        t.Data = make([]int, 0)
    }

    type my Test
    return bson.Marshal((*my)(t))
}

func main() {
    h := Test{}
    data, _ := bson.Marshal(&h)
    log.Print(bson.Raw(data))
}

// output: 
// 2009/11/10 23:00:00 t.Data is nil
// 2009/11/10 23:00:00 {"data": []}

当您保存值演示Go Playground链接 :

also, you can check this link:

Implement bson.Marshaler, and your MarshalBSON() function will be called when you save values

package main

import (
    "log"

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

type Test struct {
    Data []int `json:"data" bson:"data"`
}

func (t *Test) MarshalBSON() ([]byte, error) {
    if t.Data == nil {
        log.Println("t.Data is nil")
        t.Data = make([]int, 0)
    }

    type my Test
    return bson.Marshal((*my)(t))
}

func main() {
    h := Test{}
    data, _ := bson.Marshal(&h)
    log.Print(bson.Raw(data))
}

// output: 
// 2009/11/10 23:00:00 t.Data is nil
// 2009/11/10 23:00:00 {"data": []}

demo go playground link: https://go.dev/play/p/1WlO_44hnco

also, you can check this link: Autofill created_at and updated_at in golang struct while pushing into mongodb

末が日狂欢 2025-02-16 23:57:22

您可以使用MGOCOMPAT软件包为您的集合(或数据库)定义注册表。

import "go.mongodb.org/mongo-driver/bson/mgocompat"

...........
    
db := client.Database(dbName, options.Database().SetRegistry(mgocompat.NewRegistryBuilder().Build()))

You can define a registry for your collection (or database) using mgocompat package.

import "go.mongodb.org/mongo-driver/bson/mgocompat"

...........
    
db := client.Database(dbName, options.Database().SetRegistry(mgocompat.NewRegistryBuilder().Build()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文