Golang Mongodb %!(EXTRA

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

Golang Mongodb %!(EXTRA

问题

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

package main

import (
    "utils"
    "hash/fnv"
    "log"
    "gopkg.in/mgo.v2"
    "encoding/json"
)

type Test struct {
    Id   uint32
    Name string
}

func ConnectDB() *mgo.Session {
    session, err := mgo.Dial("localhost:27017")
    if err != nil {
        panic(err)
    }
    return session
}

func SaveMgoDoc(dbName string, collectionName string, file Test) bool {
    session, err := mgo.Dial("localhost:27017")
    
    if err != nil {
        panic(err)
    }
    
    defer session.Close()

    fileJson, err := json.Marshal(file)

    if err != nil {
        log.Printf("failed to marshal struct to json...\n", file)
        return false
    }

    collection := session.DB(dbName).C(collectionName)
    err         = collection.Insert(&fileJson)

    if err != nil {
        log.Printf("failed to insert doc into database...\n", file)
        return false
    }

    return true
}

func hash(s string) uint32 {
        h := fnv.New32a()
        h.Write([]byte(s))
        return h.Sum32()
}

func main() {
    utils.SaveMgoDoc("mydb", "mydoc", Test{hash("Me"), "Me"})
}

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

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.

package main
import (
"utils"
"hash/fnv"
"log"
"gopkg.in/mgo.v2"
"encoding/json"
)
type Test struct {
Id   uint32
Name string
}
func ConnectDB() *mgo.Session {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
return session
}
func SaveMgoDoc(dbName string, collectionName string, file Test) bool {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
defer session.Close()
fileJson, err := json.Marshal(file)
if err != nil {
log.Printf("failed to marshal struct to json...\n", file)
return false
}
collection := session.DB(dbName).C(collectionName)
err         = collection.Insert(&fileJson)
if err != nil {
log.Printf("failed to insert doc into database...\n", file)
return false
}
return true
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func main() {
utils.SaveMgoDoc("mydb", "mydoc", Test{hash("Me"), "Me"})
}

答案1

得分: 2

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

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

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

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:

确定