bsonDocToMap converts a bson.D or bson.M value to map[string]any for JSON marshaling. In MongoDB Go driver v2, nested documents decode as bson.D instead of bson.M, so we recursively convert all nested bson.D values to maps.
(val any)
| 98 | // In MongoDB Go driver v2, nested documents decode as bson.D instead of bson.M, |
| 99 | // so we recursively convert all nested bson.D values to maps. |
| 100 | func bsonDocToMap(val any) map[string]any { |
| 101 | switch v := val.(type) { |
| 102 | case bson.M: |
| 103 | for key, elem := range v { |
| 104 | v[key] = convertBSONValue(elem) |
| 105 | } |
| 106 | return v |
| 107 | case bson.D: |
| 108 | m := make(map[string]any, len(v)) |
| 109 | for _, e := range v { |
| 110 | m[e.Key] = convertBSONValue(e.Value) |
| 111 | } |
| 112 | return m |
| 113 | default: |
| 114 | return nil |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // convertBSONValue recursively converts bson.D and bson.A values to Go maps and slices. |
| 119 | func convertBSONValue(val any) any { |