这是我的结构体:
type Article struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Title string `json:"title"`
Author string `json:"author"`
Date string `json:"date"`
Tags string `json:"tags"`
Content string `json:"content"`
Status string `json:"status"`
}
这是我获取 mongodb 中数据的函数:
func AllArticles() []Article {
articles := []Article{}
err := c_articles.Find(bson.M{}).All(&articles)
if err != nil {
panic(err)
}
fmt.Println(articles)
return articles
}
这是打印结果:
[{ObjectIdHex("") Hello1 DYZ 2013-11-10 abc This is another content. published} {ObjectIdHex("") Hello2 DYZ 2013-11-14 abc This is the content. published} {ObjectIdHex("") Hello3 DYZ 2013-11-15 abc This is the content haven't been published. draft}]
可以看到,对应_id
的值是ObjectIdHex("")
,但是在数据库中的数据是这样的:
{ "_id" : ObjectId("5281b83afbb7f35cb62d0834"), "title" : "Hello1", "author" : "DYZ", "date" : "2013-11-10", "tags" : "abc", "content" : "This is another content.", "status" : "published" }
即使我把筛选条件变成:
err := c_articles.Find(bson.M{"_id": bson.ObjectIdHex("5281b83afbb7f35cb62d0834")}).All(&articles)
打印出来的数据仍然是上面那样,没有值。这是为什么?
Id bson.ObjectId
json:"id" bson:"_id"
Adakah tab digunakan dalam ruang antara json dan bson?