无法使用golang的mgo库检索“_id”值

huangapple go评论151阅读模式
英文:

Cannot retrieve "_id" value using mgo with golang

问题

这是我的结构定义:

type Article struct {
    Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`
    Title   string        `json:"title"`
    Author  string        `json:"author"`
    Date    string        `json:"date"`
    Tags    string        `json:"tags"`
    Content string        `json:"content"`
    Status  string        `json:"status"`
}

这是我从数据库获取数据的方法:

func AllArticles() []Article {
    articles := []Article{}
    err := c_articles.Find(bson.M{}).All(&articles)
    if err != nil {
        panic(err)
    }

    return articles
}

这是数据库中存储的一个对象:

{
    "_id" : ObjectId("5281b83afbb7f35cb62d0834"),
    "title" : "Hello1",
    "author" : "DYZ",
    "date" : "2013-11-10",
    "tags" : "abc",
    "content" : "This is another content.",
    "status" : "published"
}

这是打印的结果:

[{ObjectIdHex("") Hello1 DYZ 2013-11-10 abc This is another content. published} {ObjectIdHex("") Hello2 DYZ 2013-11-14 abc This is the content. published}]

看起来我无法获取 _id 字段的真实值,它总是 ""。问题出在哪里?

英文:

This is my struct definition:

type Article struct {
    Id      bson.ObjectId `json:"id"		bson:"_id,omitempty"`
    Title   string        `json:"title"`
    Author  string        `json:"author"`
    Date    string        `json:"date"`
    Tags    string        `json:"tags"`
    Content string        `json:"content"`
    Status  string        `json:"status"`
}

This is the method I get my data from database:

func AllArticles() []Article {
    articles := []Article{}
    err := c_articles.Find(bson.M{}).All(&articles)
    if err != nil {
        panic(err)
    }

    return articles
}

This is one piece of object stored in database:

{ "_id" : ObjectId( "5281b83afbb7f35cb62d0834" ),
  "title" : "Hello1",
  "author" : "DYZ",
  "date" : "2013-11-10",
  "tags" : "abc",
  "content" : "This is another content.",
  "status" : "published" }

This is the printed result:

[{ObjectIdHex("") Hello1 DYZ 2013-11-10 abc This is another content. published}     {ObjectIdHex("") Hello2 DYZ 2013-11-14 abc This is the content. published}]

It seems that I can't get the real value of _id field, it's always "". What's the problem?

答案1

得分: 44

我找到了问题。

在代码中:

Id      bson.ObjectId `json:"id"        bson:"_id,omitempty"`

json:bson: 之间,我使用了一个 tab 而不是 空格,所以出现了问题。如果我将这行代码改为:

Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`

json:bson: 之间只有一个 空格,它就可以正常工作了。

英文:

I've found the problem.

In the code:

Id      bson.ObjectId `json:"id"        bson:"_id,omitempty"`

between json: and bson:, I used a tab instead of space so the problem occurs. If I change this line of code to:

Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`

With one space between json: and bson:, it turns out to work just fine.

答案2

得分: 9

我遇到了同样的问题,并且找出了问题所在:我的导入语句搞混了。我猜想Gustavo无法重现这个问题,因为你没有提供你的导入语句,而他正确地填写了它们。

简单解释一下我的问题相似之处:

这段代码 -

err := db.Find(bson.M{"_id": bson.ObjectIdHex(userId)}).One(&user)

对我来说不起作用,它无法从数据库中获取信息,并返回 -

{ObjectIdHex("")    }

我是如何解决的...我们发现在server.go页面中,其中一个导入语句是这样的:

"gopkg.in/mgo.v2"

应该改成这样:

"labix.org/v2/mgo"

真正的错误不是使用了gopkg.in/mgo.v2,而是代码混合了labix.org/和gopkg.in导入模块。

所以诀窍是使用这个:

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"

或者这个:

"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"

但不要混合使用它们。前者是首选,因为最新的文档使用的就是这个。

希望对你有帮助。

英文:

I had the same issue and was able to figure out that I had my imports mixed up. I have a feeling that Gustavo could not reproduce the problem because you had not included what your imports looked like and he filled them out correctly.

Just to explain briefly how my issue was similar:

This -

err := db.Find(bson.M{"_id": bson.ObjectIdHex(userId)}).One(&user)

was not working for me, it would not get the info from the database and would return this-

{ObjectIdHex("")    }

How I fixed it...we found that

In the server.go page, one of the imports was this.

"gopkg.in/mgo.v2”

Should have been this.

"labix.org/v2/mgo”

The real bug is not the use of the gopkg.in/mgo.v2. It is that the code was mixing labix.org/ and gopkg.in import modules.

So the trick is to use this.

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson”

Or this.

"labix.org/v2/mgo"
"labix.org/v2/mgo/bson”

But not mix them. The top one is the preferred one, as that is what the latest docs use.

Hope this helps.

答案3

得分: 3

你的代码没问题。

这里是一个包含你的代码的自包含示例,没有修改:

这是输出结果:

"R\x94\xa4J\xff&\xc61\xc7\xfd%\xcc" "Some Title"

问题可能出在其他地方。例如,集合可能确实没有_id字段。

英文:

Your code is fine.

Here is a self-contained example that includes your code, unmodified:

And here is the output:

"R\x94\xa4J\xff&\xc61\xc7\xfd%\xcc" "Some Title"

The issue is elsewhere. The collection may really not have an _id field, for example.

huangapple
  • 本文由 发表于 2013年11月26日 19:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/20215510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定