'Insert map[string]interface{} from Golang with date as mongo date
A document for MongoDB was converted as Bson.M, which actually corresponds to a map[string]interface{} in Golang. To be generic we need a map[string]interface{}.
Date formats are stored as a string in the database, instead of a mongo date object. I've done conversions iterating over the entire map[string]interface{} which has allowed the date formats to be corrected. The date format is correct, for examle: 2022-05-01T22:36:24.5383833+02:00. If someone want, i can of course provide this solution. Anyway, it is a kind of hacking.
Is there a more elegant way? For example, is it possible to do the schema validation in GOlang and have the date formats converted correctly right away? Schema Validation
var valuesForDatabase map[string]interface{}
_ = json.Unmarshal(value, &valuesForDatabase)
// Save to MongoDB
_, err = clients.MongoClient.Database("databasename").Collection("collection").InsertOne(context.TODO(), valuesForDatabase)
if err != nil {
log.Println(err.Error())
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
