I'm trying to search for this document:
"meta": {
"pageId": "...",
"userId": "...",
"ver": "0",
},
"dialog": {
...
}
and get the entire "dialog"
as a JSON, so I created this struct:
type Dialog struct {
Dialog bson.Raw `json:"dialog" bson:"dialog"`
}
So I query the document like this:
dialog := Dialog{}
query := c.Find(locate).One(&dialog)
and when I print dialog
, I get a bunch of numbers, which I believe are the raw bytes from the query.
The question is: how to unmarshal it into a JSON object?
The only thing I've found about this are Marshal into a bson.Raw (which doesn't explain how to unmarshal into a json)
Update
Following How to marshal json string to bson document in golang for writing to MongoDB?, I did:
fmt.Println(bson.UnmarshalJSON(dialog.Dialog.Data, &a))
which gets me:
json: unknown constant "l"
As you can see I had to extract the Data
from the Raw type, I don't think this is the best way to do it since there's the Kind
field which is not being used. Also, what's this 'l'?
Update 2
I thought I had to Unmarshal into a JSON type in order to work with it but I've found that's better to Unmarshal to a map directly, so here it is:
var a bson.M
fmt.Println(bson.Unmarshal(dialog.Dialog.Data, &a))
fmt.Println(a)
It worked for me :)
However, I'm still ignoring the Kind
field of the Raw
type. Is there a better way to do it?