dth2331 2016-05-20 11:11
浏览 60
已采纳

如何在mgo中对数组元素进行多级拉取?

I want to do a multi-level array element delete. My Structs are as follows:-

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

type ProcessItem  struct{
    SortOrder       int
    Documents       []DocumentTemplate
}

type DocumentTemplate struct {
    Id              bson.ObjectId `bson:"_id,omitempty"`
    TemplateName    string
}

I want to delete an object of type DocumentTemplate. The DocumentTemplate is a struct array in ProcessItem which is a struct array in Company struct. I have Company Id(field of struct Company) and TemplateName(field of struct DocumentTemplate).

I tried the below mgo pull query but it is not working.

c := db.C("company")
pullQuery := bson.M{"process": bson.M{"documents.templatename": "xyz"}}
err := c.Update(bson.M{"_id": "123"}, bson.M{"$pull": pullQuery})

Please point out the mistakes I made here. Thanks.

Edit: Adding one example document for the clarity of the question

{
    "_id" : ObjectId("573da7dddd73171e42a84045"),
    "companyname" : "AAA",
    "process" : [ 
        {
            "processname" : "Enquiry",
            "sortorder" : 0,
            "documents" : [ 
                {
                    "templatename" : "xyz",
                    "processname" : "Enquiry"
                }, 
                {
                    "templatename" : "ss",
                    "processname" : "Enquiry"
                }
            ]
        }, 
        {
            "processname" : "Converted",
            "processtype" : 1,
            "sortorder" : 2,
            "documents" : [ 
                {
                    "templatename" : "dd",
                    "processname" : "Converted"
                }, 
                {
                    "templatename" : "fg",
                    "processname" : "Converted"
                }
            ]
        }
    ]
}

I need to pull out just one DocumentTemplete record, like the one below:

{
   "templatename" : "xyz",
   "processname" : "Enquiry"
}

N.B: TemplateName will be unique inside a Company.

  • 写回答

2条回答 默认 最新

  • duanliang1898 2016-05-20 12:10
    关注

    You'll need to use the $ positional operator (https://docs.mongodb.com/manual/reference/operator/projection/positional/). In order to be able to use that you'll also have to add to your query the following:

    "process.documents.templatename": "xyz"
    

    Your Update statement should look like this:

    c := db.C("company")
    pullQuery := bson.M{"process.$.documents": bson.M{"templatename": "xyz"}}
    err := c.Update(bson.M{"_id": "123", "process.documents.templatename": "xyz"}, bson.M{"$pull": pullQuery})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么