如何向 Mongodb 中的数组添加更多字段,Go?

How to add more fields to an array in Mongodb, Go?

这些是我的 Mongodb 文档结构。

type Company struct {
Id              bson.ObjectId `bson:"_id,omitempty"`
Company_name    string
Admin           UserMinimal
Process         []ProcessItem
}

type ProcessItemMinimal  struct {
Id              bson.ObjectId `bson:"_id,omitempty"`
Process_name    string
Processtype     int64   
}

type ProcessItem  struct{
ProcessItemMinimal  `bson:",inline"`
Sortorder           int64   
}

这是我的 mongodb 文档。

{
    "_id" : ObjectId("56cd99109096f3b762f4f149"),
    "company_name" : "xyz",
    "admin" : {
        "email" : "kk@kk.kk",
        "fullname" : "kk"
    },
    "process" : [ 
        {
            "process_name" : "Enquiry",
            "processtype" : NumberLong(0),
            "sortorder" : NumberLong(0)
        }, 
        {
            "process_name" : "Converted",
            "processtype" : NumberLong(1),
            "sortorder" : NumberLong(1)
        }, 
        {
            "process_name" : "MileStone 1",
            "processtype" : NumberLong(1),
            "sortorder" : NumberLong(2)
        }
    ]
}

我需要再添加一个 "process" 来处理数组。可能吗?如果是,我如何在 mgo 中查询?

要向数组中插入另一个文档,请使用 $push

在mgo中,

// Create the new 'ProcessItem' document you want to insert.
newProcess := ProcessItem {
    ProcessItemMinimal : processItem,
    SortOrder          : sortOrder
}

change := bson.M {
    "$push": bson.M {
        "process": newProcess,
    },
}

// Update the necessary 'Company' document
companyCollection.UpdateId(company.ID, change)