Einfügen von Daten in MongoDB mit mgo

Ich versuche, fügen Sie einige Daten in MongoDB mit Gehen.

Hier ist die Daten-Struktur:

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"`
}

Dies ist meine Funktion einfügen:

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)
}

Und schließlich, das ist, wie ich es nennen:

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)
}

Das Problem ist, ich hatte erwartet, meine bson - tags, um magisch zu arbeiten, aber Sie nicht.
Anstelle der Daten gespeichert:

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

Es wird gespeichert als:

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

Wie kann ich dieses Problem beheben?

Schreibe einen Kommentar