This is my structure 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 from the database:
func AllArticles() []Article { articles := []Article{} err := c_articles.Find(bson.M{}).All(&articles) if err != nil { panic(err) } return articles }
This is one part of the object stored in the 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 cannot get the real value of the _id field, always "" . What is the problem?
mongodb go mgo
dyzdyz010 Nov 26 '13 at 11:12 2013-11-26 11:12
source share