Question:
I have the following code, making use of go.mongodb.org/mongo-driver libraries:What is going on here?
edit: and of course, it should be noted that without a type assertion here, it doesn’t compile:
invalid operation: cannot index updateMap["$addFields"] (map index expression of type interface{})
Answer:
bson.M
is defined as an alias of primitive.M
, which is defined as type M map[string]interface{}
(that is, a non-alias type). The problem is that updateMap["$set"]
is a bson.M
, which is a distinct type compared to map[string]interface{}
. So your type assertion would have to be to bson.M
instead, for example:If you have better answer, please add a comment about this, thank you!