# NULL data type in MongoDB

**URL:** <https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501>\
**Category:** Getting Help\
**Created:** [May 25, 2017, 6:22pm UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501 "2017-05-25T18:22:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanmuga\_Mahalingam](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@shanmuga\_Mahalingam](https://forum.golangbridge.org/u/shanmuga_Mahalingam)\
**Post date:** [May 25, 2017, 6:22pm UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/1 "2017-05-25T18:22:26Z")

</div>

What’s the Go data type i need to use to insert a value in mongoDB of type Null ?.

I have following struct  
type Case struct {  
Sender string `bson:"Sender"`  
Reason string `bson:"Reason"` . —\> I need to insert this value as null and type as Null in MongoDB  
}

What data type i should declare in Golang to achieve this?. You help is appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![nathankerr](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nathankerr/32/2287_2.png) [@nathankerr](https://forum.golangbridge.org/u/nathankerr)\
**Post date:** [May 25, 2017, 6:55pm UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/2 "2017-05-25T18:55:35Z")

</div>

My guess is `*string`.

---

<div class="post-metadata">

**Author:** ![shanmuga\_Mahalingam](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@shanmuga\_Mahalingam](https://forum.golangbridge.org/u/shanmuga_Mahalingam)\
**Post date:** [May 25, 2017, 8:56pm UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/3 "2017-05-25T20:56:43Z")

</div>

@nathankerr Thanks. It worked

---

<div class="post-metadata">

**Author:** ![Rio\_M](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/rio_m/32/1214_2.png) [@Rio\_M](https://forum.golangbridge.org/u/Rio_M)\
**Post date:** [May 26, 2017, 9:32am UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/4 "2017-05-26T09:32:09Z")

</div>

Far better is avoid null. Empty string is better than pointer.

---

<div class="post-metadata">

**Author:** ![nathankerr](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nathankerr/32/2287_2.png) [@nathankerr](https://forum.golangbridge.org/u/nathankerr)\
**Post date:** [May 26, 2017, 9:46am UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/5 "2017-05-26T09:46:35Z")

</div>

Empty string does not let you differentiate between when the string was not set and when it is actively set to `""`. Null is one method to determine the difference.

Another example that also explains why you might need to make this difference is:

> **[How to determine if a JSON key has been set to null or not provided](https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/)**
>
> Learn to distinguish between when a JSON key is set to null or the key wasn't provided at all when dealing with JSON in Go.

---

<div class="post-metadata">

**Author:** ![Rio\_M](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/rio_m/32/1214_2.png) [@Rio\_M](https://forum.golangbridge.org/u/Rio_M)\
**Post date:** [May 26, 2017, 10:43am UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/6 "2017-05-26T10:43:44Z")

</div>

Yes, I know. But often I do not need this information: difference between empty and nil. And often is this information important (for example information about sex: male, female, NIL) and is better add a new special value for NIL value (male, female, unknown).

And in Go I must create pointers when I want manage nil values and it can be place for create a lot of bugs. This reason, why I want avoid pointers (and nil values) if it is possible.

---

<div class="post-metadata">

**Author:** ![nathankerr](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nathankerr/32/2287_2.png) [@nathankerr](https://forum.golangbridge.org/u/nathankerr)\
**Post date:** [May 26, 2017, 11:03am UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/7 "2017-05-26T11:03:46Z")

</div>

Pointers are not the only way to manage nil/null values in Go. The article I linked to tracks the validity of the value as part of the type. The [`sql.Null*`](https://godoc.org/database/sql#NullBool) types use a similar method.

I also try to avoid nulls and pointers when possible because they are fundamentally more complicated than non-nullable types. I also don’t shy away from them when they are needed.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 24, 2017, 11:14am UTC](https://forum.golangbridge.org/t/null-data-type-in-mongodb/5501/8 "2017-08-24T11:14:37Z")

</div>

Closed on August 24, 2017, 11:14am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
