亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Mongodb, Go 中向數組添加更多字段?

如何在 Mongodb, Go 中向數組添加更多字段?

Go
夢里花落0921 2021-12-20 09:51:30
這些是我的 Mongodb 文檔結構。type Company struct {Id              bson.ObjectId `bson:"_id,omitempty"`Company_name    stringAdmin           UserMinimalProcess         []ProcessItem}type ProcessItemMinimal  struct {Id              bson.ObjectId `bson:"_id,omitempty"`Process_name    stringProcesstype     int64   }type ProcessItem  struct{ProcessItemMinimal  `bson:",inline"`Sortorder           int64   }這是我的 mongodb 文檔。{    "_id" : ObjectId("56cd99109096f3b762f4f149"),    "company_name" : "xyz",    "admin" : {        "email" : "[email protected]",        "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)        }    ]}我需要再添加一個“進程”來處理數組。是否可以?如果是,我如何在 mgo 中查詢?
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

要將另一個文檔插入數組,請使用$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)


查看完整回答
反對 回復 2021-12-20
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號