duanlun1955
2016-02-25 09:00
浏览 695

如何在Mongodb中向数组添加更多字段?

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?

图片转代码服务由CSDN问答提供 功能建议

这些是我的Mongodb文档结构。

  type公司结构 {
Id bson.ObjectId`bson:“ _ id,omitempty”`
Company_name字符串
Admin UserMinimal 
Process [] ProcessItem 
} 
 
type ProcessItemMinimal struct {
Id bson.ObjectId`bson:“ _ id,omitempty”`  
Process_name字符串
Processtype int64 
} 
 
type ProcessItem struct {
ProcessItemMinimal`bson:“,inline”`
Sortorder int64 
} 
   
 
 

此 是我的mongodb文档。

  {
“ _id”:ObjectId(“ 56cd99109096f3b762f4f149”),
“ company_name”:“ xyz”,
“ admin”:{  
“电子邮件”:“ kk@kk.kk”,
“全名”:“ kk” 
},
“进程”:[
 {
“进程名称”:“查询”,
“  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条回答 默认 最新

  • du6jws6975 2016-02-25 10:06
    已采纳

    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)
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题