我应该如何使用mgo处理UUID字段? [英] How should I handle UUID fields using mgo?
问题描述
我在MongoDB中有此文档:
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"
}
}
我要过滤&使用mgo在Golang中获取一些文档,这是我的代码:
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
问题:
job.Key
(MongoDB Document中的j
字段是uuid)保持为空.我也尝试过"github.com/satori/go.uuid"
,但无法弄清楚.
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.
所以我想知道如何处理该uuid字段,更一般地讲如何调试此问题.在Go中完成新手培训.
So I would like to know how to handle that uuid field, and more generally how to debug this problem. Complete newbie in Go.
例如,在python中,我可以获得一个文档,而在doc._data
中,我可以看到该文档的所有字段,在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?
更新
我尝试将Key设置为bson.Raw
,我看到了一些字节,但是无法将它们转换为uuid:
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)
输出:
{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
推荐答案
感谢@Thomas,我发现我正在使用0x05
Kind获取bin数据.
Thanks to @Thomas I've figured out that I'm getting bin data with 0x05
Kind.
所以我将Job
结构更改为:
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.
这篇关于我应该如何使用mgo处理UUID字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!