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

huangapple go评论95阅读模式
英文:

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数组中添加一个新的"process"。这可能吗?如果可以,我该如何在mgo中查询它?

英文:

These are my Mongodb document structs.

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   
}

This is my mongodb document.

{
    "_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)
        }
    ]
}

I need to add one more "process" to process array. Is it possible? If yes, how can I query that in mgo?

答案1

得分: 2

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

在mgo中,

// 创建要插入的新的 'ProcessItem' 文档。
newProcess := ProcessItem {
    ProcessItemMinimal : processItem,
    SortOrder          : sortOrder
}

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

// 更新必要的 'Company' 文档
companyCollection.UpdateId(company.ID, change)
英文:

To insert another document to the array use $push

In 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)

huangapple
  • 本文由 发表于 2016年2月25日 17:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35622576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定