Inserting data into MongoDB with mgo

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

Inserting data into MongoDB with mgo

问题

我正在尝试使用Go向MongoDB插入一些数据。

这是数据结构:

type Entry struct {
    Id          string `json:"id",bson:"_id,omitempty"`
    ResourceId  int    `json:"resource_id,bson:"resource_id"`
    Word        string `json:"word",bson:"word"`
    Meaning     string `json:"meaning",bson:"meaning"`
    Example     string `json:"example",bson:"example"`
}

这是我的插入函数:

func insertEntry(db *mgo.Session, entry *Entry) error {
    c := db.DB(*mongoDB).C("entries")
    count, err := c.Find(bson.M{"resourceid": entry.ResourceId}).Limit(1).Count()
    if err != nil {
        return err
    }
    if count > 0 {
        return fmt.Errorf("resource %s already exists", entry.ResourceId)
    }
    return c.Insert(entry)
}

最后,这是我调用它的方式:

entry := &Entry{
    ResourceId:  resourceId,
    Word:        word,
    Meaning:     meaning,
    Example:     example,
}
err = insertEntry(db, entry)
if err != nil {
    log.Println("Could not save the entry to MongoDB:", err)
}

问题是,我原本期望我的bson标签能够自动工作,但实际上并没有。
保存的数据并不是我期望的形式:

{ "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "resource_id" :
7660708, "word" : "Foo" ...}

而是保存为:

{ "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "id" : "",
"resourceid" : 7660708, "word" : "Foo"...}

我该如何修复这个问题?

英文:

I'm trying to insert some data in MongoDB using Go.

Here is the data struct:

type Entry struct {
    Id          string `json:"id",bson:"_id,omitempty"`
    ResourceId  int    `json:"resource_id,bson:"resource_id"`
    Word        string `json:"word",bson:"word"`
    Meaning     string `json:"meaning",bson:"meaning"`
    Example     string `json:"example",bson:"example"`
}

This is my insert function:

func insertEntry(db *mgo.Session, entry *Entry) error {
    c := db.DB(*mongoDB).C("entries")
    count, err := c.Find(bson.M{"resourceid": entry.ResourceId}).Limit(1).Count()
    if err != nil {
        return err
    }
    if count > 0 {
        return fmt.Errorf("resource %s already exists", entry.ResourceId)
    }
    return c.Insert(entry)
}

And finally, this is how I call it:

entry := &Entry{
    ResourceId:  resourceId,
    Word:        word,
    Meaning:     meaning,
    Example:     example,
}
err = insertEntry(db, entry)
if err != nil {
    log.Println("Could not save the entry to MongoDB:", err)
}

The trouble is, I was expecting my bson tags to magically work, but they don't.
Instead of data being saved as:

> { "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "resource_id" :
> 7660708, "word" : "Foo" ...}

It gets saved as:

> { "_id" : ObjectId("53700d9cd83e146623e6bfb4"), "id" : "",
> "resourceid" : 7660708, "word" : "Foo"...}

How can I fix this?

答案1

得分: 15

将条目更改为:

type Entry struct {
    Id          string `json:"id" bson:"_id,omitempty"`
    ResourceId  int    `json:"resource_id" bson:"resource_id"`
    Word        string `json:"word" bson:"word"`
    Meaning     string `json:"meaning" bson:"meaning"`
    Example     string `json:"example" bson:"example"`
}

结构标签的语法不使用逗号分隔标签。我相信这样应该可以解决问题。

英文:

Change entry to:

type Entry struct {
    Id          string `json:"id" bson:"_id,omitempty"`
    ResourceId  int    `json:"resource_id" bson:"resource_id"`
    Word        string `json:"word" bson:"word"`
    Meaning     string `json:"meaning" bson:"meaning"`
    Example     string `json:"example" bson:"example"`
}

The syntax for Struct Tags does not use commas between tags. I believe this should fix it.

答案2

得分: 8

type Entry struct {
Id bson.ObjectId bson:"_id,omitempty" json:"id"
ResourceId int json:"resource_id" bson:"resource_id"
Word string json:"word"
Meaning string json:"meaning"
Example string json:"example"
}

可以使用UpsertId代替Count()和Insert(),它会执行相同的操作,如果存在Id,则替换记录,如果不存在,则插入。

使用空的ObjectId进行Insert(),让MongoDB处理Id的分配。

编辑:
我误读了你的Count查询。
你还有一个错误。
应该是"resource_id"而不是"resourceid",因为你声明了bson字段的名称为"resource_id"。

英文:
type Entry struct {
    Id          bson.ObjectId `bson:"_id,omitempty" json:"id"`
    ResourceId  int           `json:"resource_id" bson:"resource_id"`
    Word        string        `json:"word"`
    Meaning     string        `json:"meaning"`
    Example     string        `json:"example"`
}

Instead of Count() and Insert() you can use UpsertId which does just that, if an Id exists the record is replaced if not it's inserted.

Insert() with an empty ObjectId lets MongoDB handle Id assignment.

Edit:
Misread your Count query.
You also have an error in there.
It should be "resource_id" not "resourceid" because you declared that the bson field is named "resource_id"

答案3

得分: 0

更新你的Entry结构体如下:

type Entry struct {
    Id          string `bson:"_id"`
    ResourceId  int    `json:"resource_id,omitempty"`
    Word        string `json:"word,omitempty"`
    Meaning     string `json:"meaning,omitempty"`
    Example     string `json:"example,omitempty"`
}

这样应该可以工作!

英文:

Update your Entry struct as:

type Entry struct {
    Id          string `bson:"_id"`
    ResourceId  int    `json:"resource_id,omitempty"`
    Word        string `json:"word,omitempty"`
    Meaning     string `json:"meaning,omitempty"`
    Example     string `json:"example,omitempty"`
}

This should work!

huangapple
  • 本文由 发表于 2014年5月12日 07:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/23599235.html
匿名

发表评论

匿名网友

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

确定