如何在Golang结构中使用temptempty标志更新Mongodb字段 [英] How to update Mongodb fields with omitempty flag in Golang structure

查看:69
本文介绍了如何在Golang结构中使用temptempty标志更新Mongodb字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Coupon表单,其中有一些可选字段.

I am working on a Coupon form in which I have some optional fields.

简介:

所有表单字段值都以JSON形式接收并映射到Golang结构中.在结构中,我为每个字段添加了一个"omitempty"标志.因此,只有那些具有适当值的表单值才被映射,该结构将忽略其余的值,例如0,","false".

All the form field values are received as JSON and mapped into a Golang structure. In the structure, I have added an "omitempty" flag with every field. So only those form values are mapped which have some appropriate value, rest of the values like 0, " ", false are ignored by the structure.

这是Golang结构

type Coupon struct {
    Id               int    `json:"id,omitempty" bson:"_id,omitempty"`
    Name             string `json:"name,omitempty" bson:"name,omitempty"`
    Code             string `json:"code,omitempty" bson:"code,omitempty"`
    Description      string `json:"description,omitempty" bson:"description,omitempty"`
    Status           bool   `json:"status" bson:"status"`
    MaxUsageLimit    int    `json:"max_usage_limit,omitempty" bson:"max_usage_limit,omitempty"`
    SingleUsePerUser bool   `json:"single_use_per_user,omitempty" bson:"single_use_per_user,omitempty"`
}

问题:

  1. 当我第一次保存此表单时,适当的表单值将保存到Mongodb中.

  1. When I save this form for the very first time, the form values that are appropriate are saved into the Mongodb.

现在,我想更新该表单,并假设有一个复选框,该复选框在保存数据时已选中.在更新表单时,未选中该复选框,并且提交了表单以进行保存.现在,由于我在结构中应用了"omitempty"标志,因此它不会将空值映射到复选框字段.由于该值未映射到结构中,因此不会将其保存到数据库中.

Now I want to update that form and suppose there is a check box, which was checked at the time of saving data. While updating form, the checkbox is unchecked and form is submitted to save. Now as I have applied "omitempty" flag in the structure, so its not mapping the empty value to the checkbox field. Since the value is not mapped into the structure, its not getting saved into the Database.

当用户第二次编辑表单时,将看到与选中的复选框相同的复选框. (但实际上,该值应更新到数据库,并且复选框应显示为未选中.)

When a user edits the form for the second time, it sees the same check box as checked. (But practically, the value should be updated to the DB and the check box should be displayed as unchecked.)

我正在REST API中使用相同的表单数据(JSON格式).在API中,在更新表单数据时,如果我仅提及那些必需的值并且不传递我不想更新的值,则MongoDB会使用提供的必需值覆盖整个文档(即使这些值是也被覆盖,我既不想更新也不想在API中传递).

I am using the same form data (in JSON format) in a REST API. In API, while updating form data, if I mention only those values which are required and don't pass the values which I don't want to update, then MongoDB is overriding the whole document with the provided required values(Even those values are also being overridden which I don't want to update as well as don't pass in the API).

要求:

将来,我想公开REST API,所以我不想在这里发生这种事情.这就是为什么我不想从结构字段中删除"omitempty"标志.

In future, I want to expose the REST API, So I don't want this thing to be happened there. That is why I don't want to remove "omitempty" flag from the structure fields.

在结构中使用omitempty标志时,是否有任何方法可以将空表单值或API数据字段保存到数据库中?

Is there any way to save the empty form values or API data fields to the DB while using omitempty flag in the structure?

谢谢!

推荐答案

bool类型的值具有2个可能的值:falsetrue.您想使用bool字段通信" 3个不同的状态,即不更新该字段,将该字段设置为false并将该字段设置为true.显然这是不可能的.

A value of bool type has 2 possible values: false and true. And you want to "communicate" 3 different states with a bool field, namely to not update the field, to set the field to false and to set the field to true. This is obviously not possible.

int值也是如此:0的值不能代表2个不同的选择,即不更新该字段并将其设置为0.

Same thing goes for int values: a value of 0 cannot represent 2 different choices, namely to not update the field and to set it to 0.

如果要将omitempty选项保留在标记值中,则要使其起作用,必须将字段更改为指针:

If you want to keep the omitempty options in the tag values, then to make it work, you have to change the fields to pointers:

type Coupon struct {
    Id               *int    `json:"id,omitempty" bson:"_id,omitempty"`
    Name             string `json:"name,omitempty" bson:"name,omitempty"`
    Code             string `json:"code,omitempty" bson:"code,omitempty"`
    Description      string `json:"description,omitempty" bson:"description,omitempty"`
    Status           *bool   `json:"status" bson:"status"`
    MaxUsageLimit    *int    `json:"max_usage_limit,omitempty" bson:"max_usage_limit,omitempty"`
    SingleUsePerUser *bool   `json:"single_use_per_user,omitempty" bson:"single_use_per_user,omitempty"`
}

它的工作方式是,如果指针为nil,它将被忽略(这是"omitempty" 选项).如果该字段是非nil指针,则将其更新为指向的值.

The way it works is that if the pointer is nil, it will be left out (this is the "omitempty" option). If the field is a non-nil pointer, it will be updated to the pointed value.

例如,如果要排除bool字段,则*bool值应为/将为nil.如果要将其设置/更新为false,则它必须是指向false值的指针.如果要将其设置/更新为true,则它必须是指向true值的指针.

So for example if you want to exclude a bool field, then the *bool value should / will be nil. If you want to set / update it to false, it must be a pointer to a false value. If you want to set / update it to true, it must be a pointer to a true value.

通常,任何类型的零值都是可能的,应将其计算在内如果将其设为指针,则仅处理它是零值"和输入中缺少",指针的nil值将表示缺少输入"情况,而非-nil指向零值的指针将表示它是零值"的情况.因此,在上面的示例中,如果string字段也可以采用空字符串值(""),那么您还必须使其成为指针.

In general, any type whose zero value is possible and should be counted with, you can only handle the "it is being the zero value" and "it is missing from the input" if you make it a pointer, and the nil value of the pointer will denote the "missing from the input" case, while a non-nil pointer to the zero value will denote the "it is being the zero value" case. So in the above example if the string fields could also take the empty string value (""), then you have to also make them pointers.

请注意,您还可以使用自定义封送处理和拆送处理逻辑来实现此目的,但这比较麻烦,并且使用指针会自动为您提供此功能.

Note that you can also achieve this using custom marshaling and unmarshaling logic, but that is more cumbersome, and using pointers gives you this automatically.

这篇关于如何在Golang结构中使用temptempty标志更新Mongodb字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆