I am totally new to MongoDB and i am trying to map the planet example data to golang structs with the original MongoDb golang driver and i would greatly appreciate the help.
I ran into a problem as i was trying to map the temperature data, which exists in the database as int32, double and null, to a golang struct.
To be specific, the planet temperature data is a sub document and has 3 fields, min, mean and max. Each field can exist in some documents as int, in some as double and in some as null. Google was no help so after that i asked ChatGPT a series of questions, the best solution was:
- use an Interface as the data type in the struct so that it can hold different data types
- use “Find” instead of “FindOne”, because i want to get multiple documents
- use the “Cursor” with “Next” to loop over the documents
- use type assertion with switch to get to the correct type and cast the data
i did not get it to work yet, but is that the best way to deal with that kind of Situation?
Any help, pointers or suggestions are most welcome.
Thank you