Golang Mongodb %!(EXTRA

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

Golang Mongodb %!(EXTRA

问题

我正在尝试将一个结构体编组为JSON,然后将其插入到我的Mongo数据库中,但是一直收到这个错误:%!(EXTRA main.Test={575590180 Me})。我做错了什么?我从另一个项目中完全复制了这段代码,该项目可以无问题地插入文档。

  1. package main
  2. import (
  3. "utils"
  4. "hash/fnv"
  5. "log"
  6. "gopkg.in/mgo.v2"
  7. "encoding/json"
  8. )
  9. type Test struct {
  10. Id uint32
  11. Name string
  12. }
  13. func ConnectDB() *mgo.Session {
  14. session, err := mgo.Dial("localhost:27017")
  15. if err != nil {
  16. panic(err)
  17. }
  18. return session
  19. }
  20. func SaveMgoDoc(dbName string, collectionName string, file Test) bool {
  21. session, err := mgo.Dial("localhost:27017")
  22. if err != nil {
  23. panic(err)
  24. }
  25. defer session.Close()
  26. fileJson, err := json.Marshal(file)
  27. if err != nil {
  28. log.Printf("failed to marshal struct to json...\n", file)
  29. return false
  30. }
  31. collection := session.DB(dbName).C(collectionName)
  32. err = collection.Insert(&fileJson)
  33. if err != nil {
  34. log.Printf("failed to insert doc into database...\n", file)
  35. return false
  36. }
  37. return true
  38. }
  39. func hash(s string) uint32 {
  40. h := fnv.New32a()
  41. h.Write([]byte(s))
  42. return h.Sum32()
  43. }
  44. func main() {
  45. utils.SaveMgoDoc("mydb", "mydoc", Test{hash("Me"), "Me"})
  46. }

你的代码看起来没有明显的错误。但是,你在插入文档时,将&fileJson传递给了collection.Insert函数,这是不正确的。fileJson已经是一个字节数组,不需要再使用&来获取指针。你可以直接传递fileJsoncollection.Insert函数,像这样:

  1. err = collection.Insert(fileJson)

请尝试修改代码并再次运行,看看问题是否解决了。

英文:

I'm trying to marshal a struct into JSON and then insert it into my Mongo database, but keep on getting this error: %!(EXTRA main.Test={575590180 Me}). What am I doing wrong? I took this code exactly from another project I worked on which could insert documents without any problems.

  1. package main
  2. import (
  3. "utils"
  4. "hash/fnv"
  5. "log"
  6. "gopkg.in/mgo.v2"
  7. "encoding/json"
  8. )
  9. type Test struct {
  10. Id uint32
  11. Name string
  12. }
  13. func ConnectDB() *mgo.Session {
  14. session, err := mgo.Dial("localhost:27017")
  15. if err != nil {
  16. panic(err)
  17. }
  18. return session
  19. }
  20. func SaveMgoDoc(dbName string, collectionName string, file Test) bool {
  21. session, err := mgo.Dial("localhost:27017")
  22. if err != nil {
  23. panic(err)
  24. }
  25. defer session.Close()
  26. fileJson, err := json.Marshal(file)
  27. if err != nil {
  28. log.Printf("failed to marshal struct to json...\n", file)
  29. return false
  30. }
  31. collection := session.DB(dbName).C(collectionName)
  32. err = collection.Insert(&fileJson)
  33. if err != nil {
  34. log.Printf("failed to insert doc into database...\n", file)
  35. return false
  36. }
  37. return true
  38. }
  39. func hash(s string) uint32 {
  40. h := fnv.New32a()
  41. h.Write([]byte(s))
  42. return h.Sum32()
  43. }
  44. func main() {
  45. utils.SaveMgoDoc("mydb", "mydoc", Test{hash("Me"), "Me"})
  46. }

答案1

得分: 2

Insert函数期望接收一个指向结构体的指针,而不是一个json字符串。所以,在这种情况下,只需使用以下代码:

  1. err = collection.Insert(&file)
英文:

Insert expects a pointer to a struct, not a json string. So, in this case, just use:

  1. err = collection.Insert(&file)

huangapple
  • 本文由 发表于 2017年2月23日 03:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/42400066.html
匿名

发表评论

匿名网友

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

确定