为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 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" : "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)        }    ]}我需要再添加一个“进程”来处理数组。是否可以?如果是,我如何在 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 关注
  • 177 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号