Cannot retrieve "_id" value using mgo with golang
Asked Answered
S

3

22

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?

Swashbuckling answered 26/11, 2013 at 11:12 Comment(0)
S
44

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.

Swashbuckling answered 23/12, 2013 at 8:10 Comment(5)
I can't believe it, I spent the afternoon trying to fix weird bug, good finding!Extortioner
LOL, solved my problem - kind of 'easy to get in problem'Alkane
Use tooling like Atom or Visual Studio which points out this mistake!Fourflusher
I am trying this problem from hours this is some thing have to document for myself. Thank youComfortable
WOW - spent a lot of time debugging this. I need to add that no whitespace allowed after the :, so for example json: "id" bson: "_id" is invalid because there is a space between the : and the ". I had to correct my code to json:"_id" bson:"_id"!Opalopalesce
T
9

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.

Thorlay answered 22/4, 2015 at 15:15 Comment(0)
B
3

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.

Backpack answered 26/11, 2013 at 13:42 Comment(2)
I thought _id is the default field of each object, isn't it?Swashbuckling
As detailed above, I've provided a working example with your code, so there's nothing wrong with it by itself. We can't fix what's not broken. If you provide a self-contained counterexample showing it not working, somebody can try to help you.Backpack

© 2022 - 2024 — McMap. All rights reserved.