If you know total struct of raw data, you can define struct for item data and unmarshal it by json. Otherwise, you can try to unmarshal it to map[string]interface{}, and access it by data["FN"], post a simple example at: https://play.golang.org/p/kB-BPevwsaE
OK great. I need to find a way to convert the _id to a string, this is what I have now. Its not working for me.
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
raw, err := cur.DecodeBytes()
var ob map[string]interface{}
_ = bson.Unmarshal(raw, &ob)
id := bson2.ObjectId(&ob["_id"])
fmt.Println(id.Hex())
if err != nil { log.Fatal(err) }
break
// do something with elem....
}
It’s very hard to precisely answer your question without more details like full code. e.g What’s the type of bson2?
What’s raw data? I think you can try an example to unmarshal it to get the objectid first. It’ better for me to get what the problem is if you supply example raw data and bson2 i mentioned before.