How to operate struct with unfixed fields in go mongodb

when I first came into contact with GO, I encountered some problems.
if it is mysql, our fields are fixed, but in mongodb, each record can have different fields. For example, the first record field is
{A B C D}
. The second record is
{A B C D E}
E and there is also an object field E {name sex address}

.

these fields can exist in mongdb because it is stored in bjson, so here"s the problem. When I created model, the struct I defined was fixed, such as
type user struct {
A string
B string
C string
D string
}

.

but this does not satisfy the second case at all, because there may be more fields in the third case, because the business needs, just like creating a struct dynamically, how can we solve such a problem? Thank you for your advice.

May.29,2022

just use the most complete one. If the old data is not available, it will be empty


take a look at the Get Started guide, which describes the use of BSON object mapping documents: https://www.mongodb.com/blog/...

.
Menu