Golang Mongo

Handle MongoDB Collection intermitten between String and Embedded Document

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func (a *Address) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
	if s, _, ok := bsoncore.ReadString(data); ok {
		a.Address = s
		return nil
	}

	if t != bson.TypeEmbeddedDocument {
		return fmt.Errorf("expected embedded document, got BSON type: %v", t)
	}

	var doc bson.D
	if err := bson.Unmarshal(data, &doc); err != nil {
		return fmt.Errorf("error unmarshaling embedded document: %v", err)
	}

	for _, elem := range doc {
		switch elem.Key {
        // List down the sub field, if Neeed to read
		case "address":
			a.Address = elem.Value.(string)
		case "kec":
			a.Kec = elem.Value.(string)
		case "kel":
			a.Kel = elem.Value.(string)
		}
	}
	return nil
}

Handle Document value datetime or string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func (cd *CustomDate) UnmarshalBSONValue(t bsontype.Type, data []byte) error {
	if s, _, ok := bsoncore.ReadDateTime(data); ok {
		ts := time.Unix(s/1000, 0).Format(time.RFC3339)
		cd.Value = ts
		return nil
	}

	if s, _, ok := bsoncore.ReadString(data); ok {
		cd.Value = s
	}
	return nil
}

Handle JSON decode with strip field

Source

1
2
3
4
5
{
    "date": {
        "Value": "2020-01-11T12:00:11"
    }
}

To

1
2
3
{
    "date": "2020-01-11T12:00:11"
}
1
2
3
func (cd CustomDate) MarshalJSON() ([]byte, error) {
	return json.Marshal(cd.Value)
}