使用 mgo 存储嵌套结构
·
问题:使用 mgo 存储嵌套结构
我正在尝试从高度嵌套的 go 结构构建一个 mongo 文档,并且我遇到了从 go 结构到 mongo 对象的转换问题。我在这里构建了一个我正在尝试使用的_very_简化版本: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}
这是完全有道理的。似乎 Write 函数或 json.Marshal 函数具有一些折叠嵌套结构的功能,但是当我尝试使用 mgo 函数func (*Collection) Upsert(http://godoc.org )将此数据插入 mongo 数据库时出现问题/labix.org/v2/mgo#Collection.Upsert)。如果我首先使用json.Marshal()函数并将字节传递给collection.Upsert(),它会以二进制形式存储,这是我不想要的,但如果我使用collection.Upsert(bson.M("_id": id, &c),它会显示为嵌套结构,形式如下:
{
"Square": {
"Length": 2
"Width": 3
}
"Depth": 4
}
但是我想要做的是使用与使用os.Stdout.Write()函数时相同的结构对 mongo 进行更新:
{
"Length":2,
"Width":3,
"Depth":4
}
我是否缺少一些可以轻松处理此问题的标志?在这一点上我能看到的唯一选择是通过删除结构的嵌套来严重降低代码的可读性,我真的很讨厌这样做。同样,我的实际代码比这个例子复杂得多,所以如果我可以通过保持嵌套来避免使其更加复杂,那肯定会更好。
解答
我认为使用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中,以防您使用它。
更多推荐
所有评论(0)