我正在与MGO合作(因为没有找到比它更好的了)。我已经玩过它并获得了一些结果,但是我不明白如何获取收到的文档的_id(内部Mongo ObjectId)?
例如:
type FunnyNumber struct {
Value int
_id string
}
session, err := mgo.Dial("127.0.0.1:27017")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
c := session.DB("m101").C("funnynumbers")
funnynumber := FunnyNumber{}
err = c.Find(bson.M{}).One(&funnynumber)
if err != nil {
log.Fatal(err)
}
fmt.Println("Id one:", funnynumber._id) // Nothing here? WHy? How to get it.
fmt.Println("Value one:", funnynumber.Value) // 62. It's OK!
有人可以帮我吗?我在哪里可以读到一些相关信息?我尚未在MGO文档中找到任何内容
我的文档的架构是:
{ "_id" : ObjectId("50778ce69331a280cf4bcf90"), "value" : 62 }
谢谢!
_id
变量更改为大写(ID)以使其可导出。bson.ObjectID
它的类型。FunnyNumber
Id变量添加标签。领域为了获得对象ID值,应完成上述三件事。
import "labix.org/v2/mgo/bson"
type FunnyNumber struct {
Value int `json:"value"`
Id bson.ObjectId `bson:"_id,omitempty"`` // only uppercase variables can be exported
}
查看BSON软件包,以更深入地了解在使用mongodb时如何使用bson标签
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句