17

我正在尝试从高度嵌套的 go 结构构建一个 mongo 文档,并且我遇到了从 go 结构到 mongo 对象的转换问题。我在这里构建了一个非常简化的版本:http ://play.golang.org/p/yPZW88deOa

package main

import (
    "os"
    "fmt"
    "encoding/json"
)

type Square struct {
    Length int 
    Width int
}

type Cube struct {
    Square
    Depth int
}

func main() {
    c := new(Cube)
    c.Length = 2
    c.Width = 3
    c.Depth = 4

    b, err := json.Marshal(c)
    if err != nil {
        panic(err)
    }

    fmt.Println(c)
    os.Stdout.Write(b)
}

运行它会产生以下输出:

&{{2 3} 4}
{"Length":2,"Width":3,"Depth":4}

这是完全有道理的。func (*Collection) Upsert似乎 Write 函数或 json.Marshal 函数具有一些折叠嵌套结构的功能,但是当我尝试使用 mgo 函数(http://godoc.org/labix)将此数据插入 mongo 数据库时,我的问题就出现了.org/v2/mgo#Collection.Upsert)。如果我json.Marshal()首先使用该函数并将字节传递给collection.Upsert(),它将以二进制形式存储,这是我不想要的,但如果我使用collection.Upsert(bson.M("_id": id, &c)它,它会显示为具有以下形式的嵌套结构:

{
    "Square": {
        "Length": 2
        "Width": 3
    }
    "Depth": 4
}

但是我想要做的是对 mongo 进行更新,其结构与我使用该os.Stdout.Write()函数时得到的相同:

{
     "Length":2,
     "Width":3,
     "Depth":4
}

我是否缺少一些可以轻松处理此问题的标志?在这一点上我能看到的唯一选择是通过删除结构的嵌套来严重降低代码的可读性,我真的很讨厌这样做。同样,我的实际代码比这个例子复杂得多,所以如果我可以通过保持嵌套来避免使其更加复杂,那肯定会更好。

4

1 回答 1

39

我认为使用inline字段标签是您的最佳选择。mgo /v2/bson 文档指出:

inline     Inline the field, which must be a struct or a map,
           causing all of its fields or keys to be processed as if
           they were part of the outer struct. For maps, keys must
           not conflict with the bson keys of other struct fields.

然后,您的结构应定义如下:

type Cube struct {
    Square `bson:",inline"`
    Depth  int
}

编辑

inline也存在于mgo/v1/bson您正在使用的情况下。

于 2013-12-31T09:04:34.047 回答