英文:
Marshal Go Struct to BSON for mongoimport
问题
我有一个结构体切片,我想将其写入一个 BSON 文件,以便进行 mongoimport
。
这是我正在做的大致想法(使用 gopkg.in/mgo.v2/bson
):
type Item struct {
ID string `bson:"_id"`
Text string `bson:"text"`
}
items := []Item{
{
ID: "abc",
Text: "def",
},
{
ID: "uvw",
Text: "xyz",
},
}
file, err := bson.Marshal(items)
if err != nil {
fmt.Printf("Failed to marshal BSON file: '%s'", err.Error())
}
if err := ioutil.WriteFile("test.bson", file, 0644); err != nil {
fmt.Printf("Failed to write BSON file: '%s'", err.Error())
}
这段代码可以运行并生成文件,但格式不正确 - 它看起来像这样(使用 bsondump --pretty test.bson
):
{
"1": {
"_id": "abc",
"text": "def"
},
"2": {
"_id": "abc",
"text": "def"
}
}
我认为它应该更像这样:
{
"_id": "abc",
"text": "def"
}
{
"_id": "abc",
"text": "def"
}
在 Go 中是否可以实现这个效果?我只是想生成一个 .bson
文件,就像你期望 mongodump
命令生成的那样,这样我就可以运行 mongoimport
命令并填充一个集合。
英文:
I have a slice of structs that I want to write to a BSON file for doing a mongoimport
.
This a rough idea of what I'm doing (using gopkg.in/mgo.v2/bson
):
type Item struct {
ID string `bson:"_id"`
Text string `bson:"text"`
}
items := []Item{
{
ID: "abc",
Text: "def",
},
{
ID: "uvw",
Text: "xyz",
},
}
file, err := bson.Marshal(items)
if err != nil {
fmt.Printf("Failed to marshal BSON file: '%s'", err.Error())
}
if err := ioutil.WriteFile("test.bson", file, 0644); err != nil {
fmt.Printf("Failed to write BSON file: '%s'", err.Error())
}
This runs and generates the file, but it's not formatted correctly - instead it looks like this (using bsondump --pretty test.bson
):
{
"1": {
"_id": "abc",
"text": "def"
},
"2": {
"_id": "abc",
"text": "def"
}
}
When I think it should look more like:
{
"_id": "abc",
"text": "def"
{
}
"_id": "abc",
"text": "def"
}
Is this possible to do in Go? I just want to generate a .bson
file that you would expect a mongodump
command to produce, so that I can run mongoimport
and populate a colection.
答案1
得分: 1
你想要独立的BSON文档,所以需要逐个进行编组:
buf := &bytes.Buffer{}
for _, item := range items {
data, err := bson.Marshal(item)
if err != nil {
fmt.Printf("无法编组BSON项:%v", err)
}
buf.Write(data)
}
if err := ioutil.WriteFile("test.bson", buf.Bytes(), 0644); err != nil {
fmt.Printf("无法写入BSON文件:%v", err)
}
运行bsondump --pretty test.bson
,输出将会是:
{
"_id": "abc",
"text": "def"
}
{
"_id": "uvw",
"text": "xyz"
}
2022-02-09T10:23:44.886+0100 找到2个对象
请注意,如果直接写入文件,则不需要使用缓冲区:
f, err := os.Create("test.bson")
if err != nil {
log.Panicf("os.Create失败:%v", err)
}
defer f.Close()
for _, item := range items {
data, err := bson.Marshal(item)
if err != nil {
log.Panicf("bson.Marshal失败:%v", err)
}
if _, err := f.Write(data); err != nil {
log.Panicf("f.Write失败:%v", err)
}
}
英文:
You want independent BSON documents, so marshal the items individually:
buf := &bytes.Buffer{}
for _, item := range items {
data, err := bson.Marshal(item)
if err != nil {
fmt.Printf("Failed to marshal BSON item: '%v'", err)
}
buf.Write(data)
}
if err := ioutil.WriteFile("test.bson", buf.Bytes(), 0644); err != nil {
fmt.Printf("Failed to write BSON file: '%v'", err)
}
Running bsondump --pretty test.bson
, the output will be:
{
"_id": "abc",
"text": "def"
}
{
"_id": "uvw",
"text": "xyz"
}
2022-02-09T10:23:44.886+0100 2 objects found
Note that the buffer is not needed if you write directly to the file:
f, err := os.Create("test.bson")
if err != nil {
log.Panicf("os.Create failed: %v", err)
}
defer f.Close()
for _, item := range items {
data, err := bson.Marshal(item)
if err != nil {
log.Panicf("bson.Marshal failed: %v", err)
}
if _, err := f.Write(data); err != nil {
log.Panicf("f.Write failed: %v", err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论