英文:
how to change the values of an array to an empty string or an empty array in golang?
问题
我有一段包含嵌套数组值的代码:
var descriptor models.Descriptor
_ = json.NewDecoder(r.Body).Decode(&descriptor)
result, _ := collectionDescriptor.InsertOne(context.TODO(), descriptor)
type Descriptor struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Version string `json:"version,omitempty" bson:"version,omitempty"`
Modules []string `json:"modules,omitempty" bson:"modules,omitempty"`
Configs []Config `json:"configs" bson:"configs"`
}
type Config struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Protocol []Protocols `json:"protocol" bson:"protocol"`
}
type Protocols struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name" bson:"name,omitempty"`
Items []Itemes `json:"items" bson:"items"`
}
descriptor.Configs[0].Protocol[0] = nil
config = descriptor.Configs[0]
_ = json.NewDecoder(r.Body).Decode(&config)
conf, errr := collectionConfigDes.InsertOne(context.TODO(), config)
json.NewEncoder(w).Encode(conf)
我想要将descriptor.configs[0].protocol的值移除,使其成为一个空数组或空字符串,然后将其发送到MongoDB。JSON形式如下所示:

英文:
I have code which contains some nested array values:
var descriptor models.Descriptor
_ = json.NewDecoder(r.Body).Decode(&descriptor)
result, _ := collectionDescriptor.InsertOne(context.TODO(), descriptor)
type Descriptor struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Version string `json:"version,omitempty" bson:"version,omitempty"`
Modules []string `json:"modules,omitempty" bson:"modules,omitempty"`
Configs []Config `json:"configs" bson:"configs"`
}
type Config struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Protocol []Protocols `json:"protocol" bson:"protocol"`
}
type Protocols struct {
Id string `json:"id" bson:"id"`
Type string `json:"type,omitempty" bson:"type,omitempty"`
Name string `json:"name" bson:"name,omitempty"`
Items []Itemes `json:"items" bson:"items"`
}
descriptor.Configs[0].Protocol[0] = nil
config = descriptor.Configs[0]
_ = json.NewDecoder(r.Body).Decode(&config)
conf, errr := collectionConfigDes.InsertOne(context.TODO(), config)
json.NewEncoder(w).Encode(conf)
I want to remove the value in descriptor.configs[0].protocol to be an empty array or an empty string. which I will then send to MongoDB. JSON form like below

答案1
得分: 0
对于空数组:
只需使用descriptor.Configs[0].Protocol[0] = make([]Protocols, 0)(不是nil)
并确保bson没有使用omitempty。
对于空字符串:
您可以创建一个临时结构体,以重写Protocol的类型。
不幸的是,bson 不能像json那样使用临时结构体:
collection.InsertOne(ctx, struct {
*Config
Protocol string `json:"protocol"`
}{
Config: &descriptor.Configs[0],
Protocol: "",
})
您应该像这样使用:
collection.InsertOne(m.ctx, struct {
Id string `bson:"id"`
Type string `bson:"type"`
Name string `bson:"name"`
Protocol string `bson:"protocol"`
}{
Id: config.Id,
Type: config.Type,
Name: config.Name,
Protocol: "",
})
英文:
To an empty array:
Just use descriptor.Configs[0].Protocol[0] = make([]Protocols, 0) (not nil)
And be sure that bson without omitempty.
To empty string:
You can create a temp struct, to rewrite the type of Protocol.
Unfortunately bson can not use temp struct like json:
collection.InsertOne(ctx, struct {
*Config
Protocol string `json:"protocol"`
}{
Config: &descriptor.Configs[0],
Protocol: "",
})
You should use like:
collection.InsertOne(m.ctx, struct {
Id string `bson:"id"`
Type string `bson:"type"`
Name string `bson:"name"`
Protocol string `bson:"protocol"`
}{
Id: config.Id,
Type: config.Type,
Name: config.Name,
Protocol: "",
})
答案2
得分: 0
如果我正确理解了问题陈述,你正在尝试从response.Body中读取JSON数据,将一个字段设置为nil/空,并将其插入到MongoDB中。如果是这样的话,那么:
- 我不确定为什么你在解码之前将字段设置为nil。应该是这样的:
config := Descriptor{}
json.NewDecoder(r.Body).Decode(&desc)
config.Protocol[0] = nil
// 或者如果解码和编码Descriptor或其切片
// 这对于更新数据库中的记录很方便
desc := Config{}
json.NewDecoder(r.Body).Decode(&config)
desc[0].Configs[0].Protocol = nil
| Before | After |
|---|---|
![]() |
![]() |
不同的方法(关于JSON)有:
1.1. 从Protocol字段中移除omitempty。这将在JSON中给出null。
Protocol []Protocols `json:"protocol" bson:"protocol"`
1.2. 像上面那样移除omitempty,并使用make,如@Sangria所提到的。这将在JSON中给出空数组/切片。
desc[0].Configs[0].Protocol = make([]Protocols, 0)
| Null Value | Empty Array |
|---|---|
![]() |
![]() |
这在JSON中是有效的。将其插入MongoDB是否有任何问题?尝试这3种可能性。
- 如果你要存储一个新记录,那么无所谓。但是如果要编辑现有记录,你应该在Mongo中使用
collection.UpdateOne()。
英文:
If I'm getting the problem statement correctly, you are trying to read the json data from response.Body, make a field nil/empty, and then insert into MongoDB. If that is what it is, then
- I'm not sure why you are making the field nil before decoding. It should be:
config := Descriptor{}
json.NewDecoder(r.Body).Decode(&desc)
config.Protocol[0] = nil
// or if decoding and encoding Descriptor or slice of it
// which is handy for updating the record in DB
desc := Config{}
json.NewDecoder(r.Body).Decode(&config)
desc[0].Configs[0].Protocol = nil
| Before | After |
|---|---|
![]() |
![]() |
Different approaches (on JSON) are,
1.1. Remove omitempty from Protocol field. This gives null in JSON.
Protocol []Protocols `json:"protocol" bson:"protocol"`
1.2. Remove omitempty like above; and use make as @Sangria mentioned. This gives empty array/slice in JSON.
desc[0].Configs[0].Protocol = make([]Protocols, 0)
| Null Value | Empty Array |
|---|---|
![]() |
![]() |
This works for JSON at least. Is there any issue to insert the same into Mongo? Try the 3 possibilities.
- Doesn't matter if you are storing the record as a new one. But in case of editing the existing, you should be using
collection.UpdateOne()in Mongo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论