Further on with the question
https://stackoverflow.com/questions/60072372/filtering-values-out-of-a-bson-m-in-golang/where the answer says to use `range server.installed` while the comment says
server.host undefined (type string has no field or method subject)
This is what I've tried with the MongoDB `[]bson.M{}` type results (obtained from like `$lookup`):
The []bson.M{} type of result I'm getting is:
[map[_id:ObjectID("65529d178688bac31b739f82") author:[map[_id:ObjectID("65529d168688bac31b739f81") created_at:1699912982379 name:Mehran updated_at:1699912982379]] author_id:ObjectID("65529d168688bac31b739f81") created_at:1699912983386 name:Test pages:124 updated_at:1699912983386]]
I've made several attempts but was unable to access the author field of the lookup result in Go.
a := []author{}
for _, val := range result {
a = append(a, val["author"].(primitive.A)[0])
}
- If using val.author, I'm getting val.author undefined (type primitive.M has no field or method author)
- If using val["author"], I'm getting invalid operation: cannot index val["author"] (map index expression of type interface{})
- If using val["author"].(primitive.A)[0], I'm getting cannot use val["author"].(primitive.A)[0] (variable of type interface{}) as author value in argument to append: need type assertion
And how to go about to fix this "need type assertion" just stumbled me, as I've tried val["author"].(primitive.A).([]main.author)[0], or []author(val["author"].(primitive.A))[0], but I'm getting invalid operation: cannot call non-function val["author"].(primitive.A) (comma, ok expression of type primitive.A)
and I've now run out of ideas how to fix it.
Please help