Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 戈朗;mgo:如何创建具有公共字段(如_id、创建时间和上次更新)的通用实体_Json_Go_Mgo - Fatal编程技术网

Json 戈朗;mgo:如何创建具有公共字段(如_id、创建时间和上次更新)的通用实体

Json 戈朗;mgo:如何创建具有公共字段(如_id、创建时间和上次更新)的通用实体,json,go,mgo,Json,Go,Mgo,给定以下结构: package models import ( "time" "gopkg.in/mgo.v2/bson" ) type User struct { Id bson.ObjectId `json:"id" bson:"_id"` Name string `json:"name" bson:"name"` BirthDate time.Time `json:"birth_date" bs

给定以下结构:

package models

import (
    "time"
    "gopkg.in/mgo.v2/bson"
)

type User struct {
    Id         bson.ObjectId `json:"id" bson:"_id"`
    Name       string        `json:"name" bson:"name"`
    BirthDate  time.Time     `json:"birth_date" bson:"birth_date"`
    InsertedAt time.Time     `json:"inserted_at" bson:"inserted_at"`
    LastUpdate time.Time     `json:"last_update" bson:"last_update"`
}
。。。以下是我如何将新用户插入Mongo集合:

user := &models.User{
    bson.NewObjectId(),
    "John Belushi",
    time.Date(1949, 01, 24),
    time.now().UTC(),
    time.now().UTC(),
}

dao.session.DB("test").C("users").Insert(user)
是否可以让一个通用的
实体
继承所有其他实体?我试过这个

type Entity struct {
    Id         bson.ObjectId `json:"id" bson:"_id"`
    InsertedAt time.Time     `json:"inserted_at" bson:"inserted_at"`
    LastUpdate time.Time     `json:"last_update" bson:"last_update"`
}

type User struct {
    Entity
    Name       string        `json:"name" bson:"name"`
    BirthDate  time.Time     `json:"birth_date" bson:"birth_date"`
}
。。。但这意味着最终结果如下:

{
    "Entity": {
        "_id": "...",
        "inserted_at": "...",
        "last_update": "..."
    },
    "name": "John Belushi",
    "birth_date": "1949-01-24..."
}
如何在不重复每个
结构中的公共字段的情况下获得以下结果

{
    "_id": "...",
    "inserted_at": "...",
    "last_update": "...",
    "name": "John Belushi",
    "birth_date": "1949-01-24..."
}

这一点已经在中得到了回答,但非常简单,您只需在匿名内部结构上添加
bson:,inline“
,并按正常方式初始化即可

下面是一个简单的例子:

package main

import (
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Entity struct {
    Id         bson.ObjectId `json:"id" bson:"_id"`
    InsertedAt time.Time     `json:"inserted_at" bson:"inserted_at"`
    LastUpdate time.Time     `json:"last_update" bson:"last_update"`
}

type User struct {
    Entity    `bson:",inline"`
    Name      string    `json:"name" bson:"name"`
    BirthDate time.Time `json:"birth_date" bson:"birth_date"`
}

func main() {
    info := &mgo.DialInfo{
        Addrs:    []string{"localhost:27017"},
        Timeout:  60 * time.Second,
        Database: "test",
    }

    session, err := mgo.DialWithInfo(info)
    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    //  c := session.DB("test").C("users")

    user := User{
        Entity:    Entity{"123456789098", time.Now().UTC(), time.Now().UTC()},
        Name:      "John Belushi",
        BirthDate: time.Date(1959, time.February, 28, 0, 0, 0, 0, time.UTC),
    }

    session.DB("test").C("users").Insert(user)
}

这一点已经在中得到了回答,但非常简单,您只需在匿名内部结构上添加
bson:,inline“
,并按正常方式初始化即可

下面是一个简单的例子:

package main

import (
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Entity struct {
    Id         bson.ObjectId `json:"id" bson:"_id"`
    InsertedAt time.Time     `json:"inserted_at" bson:"inserted_at"`
    LastUpdate time.Time     `json:"last_update" bson:"last_update"`
}

type User struct {
    Entity    `bson:",inline"`
    Name      string    `json:"name" bson:"name"`
    BirthDate time.Time `json:"birth_date" bson:"birth_date"`
}

func main() {
    info := &mgo.DialInfo{
        Addrs:    []string{"localhost:27017"},
        Timeout:  60 * time.Second,
        Database: "test",
    }

    session, err := mgo.DialWithInfo(info)
    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    //  c := session.DB("test").C("users")

    user := User{
        Entity:    Entity{"123456789098", time.Now().UTC(), time.Now().UTC()},
        Name:      "John Belushi",
        BirthDate: time.Date(1959, time.February, 28, 0, 0, 0, 0, time.UTC),
    }

    session.DB("test").C("users").Insert(user)
}
你真的试过了吗?你真的试过了吗?