I have this Document in MongoDB:
{
"_id": {
"$oid": "5ad0873b169ade0001345d34"
},
"j": {
"$uuid": "94482b86-1005-e3a0-5235-55fb7c1d648a"
},
"v": "sign",
"d": "a",
"s": "init",
"response": {},
"creation_date": {
"$date": "2018-04-13T10:32:27.140Z"
}
}
I want to filter & fetch some documents in Golang using mgo, and here's my code:
package main
import (
"fmt"
"log"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type JOB struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Key string `bson:"j"`
Svc string `bson:"v"`
DocType string `bson:"d"`
Status string `bson:"s"`
CreationDate time.Time `bson:"creation_date"`
}
func main() {
session, err := mgo.Dial("mongodb://...")
if err != nil {
log.Fatal(err)
}
defer session.Close()
c := session.DB("main").C("job")
var results []JOB
e := c.Find(bson.M{"v": "sign"}).All(&results)
if e != nil {
log.Fatal(e)
}
for _, job := range results[:5] {
fmt.Println(job.ID, job.Key, job.Svc, job.DocType, job.Status, job.CreationDate)
}
}
Here's the output when I run my program:
ObjectIdHex("5acf91e0269c650001a82683") sign a ok 2018-04-12 19:05:36.294 +0200 CEST
ObjectIdHex("5ad0873b169ade0001345d34") sign a init 2018-04-13 12:32:27.14 +0200 CEST
ObjectIdHex("5ad0873e169ade0001345d36") sign a init 2018-04-13 12:32:30.852 +0200 CEST
ObjectIdHex("5ad08742169ade0001345d38") sign a init 2018-04-13 12:32:34.478 +0200 CEST
ObjectIdHex("5ad087492e083b00013a862a") sign a init 2018-04-13 12:32:41.577 +0200 CEST
Problem:
job.Key
(j
field in MongoDB Document which is a uuid) remains empty. I've tried also "github.com/satori/go.uuid"
but I couldn't figure it out.
So I would like to know how to handle that uuid field, and more generally how to debug this problem. Complete newbie in Go.
For example in python I could get a Document and using doc._data
I could see all fields of that document, is there a equivalent way of doing this in Go?
UPDATE
I tried to set Key as bson.Raw
, I see some bytes, but cannot convert them to uuid:
fmt.Println(job.Key)
u := uuid.FromBytesOrNil(job.Key.Data)
fmt.Println(u)
Output:
{5 [16 0 0 0 3 160 227 5 16 134 43 72 148 138 100 29 124 251 85 53 82]}
00000000-0000-0000-0000-000000000000
Thanks to @Thomas I've figured out that I'm getting bin data with 0x05
Kind.
So I changed Job
struct to:
Key bson.Binary `bson:"j"`
and after doing query, I unmarshal that binary data like this:
import "github.com/satori/go.uuid"
var Ids []uuid.UUID
for _, job := range results {
u, err := uuid.FromBytes(job.Key.Data)
if err != nil {
panic(err)
}
Ids = append(Ids, u)
}
So now in Job.Key.Data
I have binary version of UUID according to this documentation.
Using the satori/go.uuid
lib you mentioned, you can accomplish this by implementing the bson.Setter interface on the type you use for the UUID field.
type Setter interface {
SetBSON(raw Raw) error
}
A minimal example could look like the following. First I define my struct, but instead of the UUID type from satori/go.uuid I instead embed that type into my own type. This allows us to define a method on it. You could also accomplish this with different type declaration such as type MyUUID uuid.UUID
but then you would need to perform a type conversion uuid.UUID(record.UUID)
to gain access to the fields and methods on the underlying uuid.UUID
type.
// MyUUID is a struct embedding the actual real UUID type
// so that we can implement bson.Setter
type MyUUID struct{ uuid.UUID }
// Record is a simplified version of what you're reading in
type Record struct {
ID int
Name string
UUID MyUUID `bson:"j"`
}
Next we implement the bson.Setter
method on MyUUID
// SetBSON lets us perform custom deserialization
func (m *MyUUID) SetBSON(raw bson.Raw) error {
// First we decode the BSON data as a anonymous J struct
var j struct {
UUID string `bson:"$uuid"`
}
err := raw.Unmarshal(&j)
if err != nil {
return err
}
// Then we use the parse the string UUID
uu, err := uuid.FromString(j.UUID)
if err != nil {
return err
}
// Finally build the result type and set it into the pointer to our record's field.
*m = MyUUID{uu}
return nil
}
It won't on the playground run due to the absence of packages, but fully functioning source that demonstrates this is available here. The example output when I run it locally:
> go run main.go
2018/06/16 14:49:49 {1 George {fdcfa79c-6b83-444e-9a91-a02f0aeaa260}}
2018/06/16 14:49:49 {1 George fdcfa79c-6b83-444e-9a91-a02f0aeaa260}
I read @sheshkovsky and @Thomas ideas and I actually came out with a copy paste solution which in my case worked.
// MongoUUID represents a UUID as saved in MongoDB
type MongoUUID struct{ uuid.UUID }
// SetBSON implements the bson.Setter interface
func (id *MongoUUID) SetBSON(raw bson.Raw) error {
// First we decode the BSON data as a UUID Binary
// Optionally we can check here if the Kind is correct
var j bson.Binary
err := raw.Unmarshal(&j)
if err != nil {
return err
}
// Then we use the parse the string UUID
uu, err := uuid.FromBytes(j.Data)
if err != nil {
return err
}
// Finally build the result type and set it into the pointer to our record's field.
*id = MongoUUID{uu}
return nil
}
// GetBSON implements the bson.Getter interface
func (id *MongoUUID) GetBSON() (interface{}, error) {
// we create an empty UUID Binary Mongo Object
ret := bson.Binary{
Kind: bson.BinaryUUID,
Data: nil,
}
// And we pass the UUID data to it
if id == nil {
ret.Data = uuid.Nil.Bytes()
} else {
ret.Data = id.Bytes()
}
// finally we return ;)
return ret, nil
}
With this solution you can directly pass a MongoUUID wrapping an uuid.UUID value like a charm :)