drxp993551 2017-12-19 22:44
浏览 13
已采纳

为什么我的查询没有更新数组内的对象?

I am trying to update an object inside an array of a document using mgo. The object structure is as following:

  1. {
  2. "_id": 2,
  3. "status": 0,
  4. "details": [{
  5. "id": 2,
  6. "category": "A",
  7. "obj": {
  8. "apple": 5,
  9. "banana": 2,
  10. "cherry": 10
  11. },
  12. "members": [{
  13. "id": 3,
  14. "category": "A",
  15. "obj": {
  16. "apple": 5,
  17. "banana": 2,
  18. "cherry": 10
  19. }
  20. },
  21. {
  22. "id": 4,
  23. "category": "A",
  24. "obj": {
  25. "apple": 5,
  26. "banana": 2,
  27. "cherry": 10
  28. }
  29. }
  30. ]
  31. }]
  32. }

Query1: I am first trying to update details > obj where trying to add another attribute "guava": 15 using the following query

  1. cond := bson.M{ "$and": []bson.M{ bson.M{"document.details":bson.M{ "$elemMatch": bson.M{ "category": "A"} } }, bson.M{"document.status":0} } }
  2. query := bson.M{ "$set": bson.M{ "document.details.$.obj.guava":15 } }
  3. _, err := models.DbUpdateAll(Collection, cond, query)

This query is neither producing any error nor updating the doc. Can anyone please tell how can I accomplish it

Note: I have searched over google but could not find relevant to what I need.

Query2: I also need to update the details > members > obj the same way I am trying to do for details > obj. Please also tell me how can I achieve the same for details > members > obj.

I have spent hours on figuring this out but nothing worked out. I shall be thankful if anyone could guide me.

展开全部

  • 写回答

1条回答 默认 最新

  • duanchifo2866 2017-12-20 00:46
    关注

    For simple, I delete the members.

    package main
    
    import (
        "fmt"
        "encoding/json"
    
        "gopkg.in/mgo.v2"
        "gopkg.in/mgo.v2/bson"
    )
    
    func main() {
        session, err := mgo.Dial("127.0.0.1")
        if err != nil {
           panic(err)
        }
    
        defer session.Close()
    
        // Optional. Switch the session to a monotonic behavior.
        session.SetMode(mgo.Monotonic, true)
        c := session.DB("test").C("mgo")
    
        cond := bson.M{"$and": []bson.M{bson.M{"details": bson.M{"$elemMatch": bson.M{"category": "A"}}}, bson.M{"status": 0}}}
        query := bson.M{"$set": bson.M{"details.$.obj.guava": 15}}
    
        res := []interface{}{}
        err = c.Find(cond).All(&res)
    
        if err != nil {
            fmt.Println("Before Update Read Error:", err)
            return
        }
    
        data, _ := json.MarshalIndent(res, "", " ")
        fmt.Printf("Before Update Read: %s
    ", string(data))
    
        err = c.Update(cond, query)
    
        if err != nil {
            fmt.Println("Update Error:", err)
            return
        }
    
        fmt.Println("Update Succeed!")
    
        err = c.Find(cond).All(&res)
        if err != nil {
            fmt.Println("After Update Read Error:", err)
            return
        }
    
        data, _ = json.MarshalIndent(res, "", " ")
        fmt.Printf("After Update Read: %s
    ", string(data))
    }
    

    Result:

    Before Update Read: [
     {
      "_id": 2,
      "details": [
       {
        "category": "A",
        "id": 2,
        "obj": {
         "apple": 5,
         "banana": 2,
         "cherry": 10
        }
       }
      ],
      "status": 0
     }
    ]
    
    Update Succeed!
    
    After Update Read: [
     {
      "_id": 2,
      "details": [
       {
        "category": "A",
        "id": 2,
        "obj": {
         "apple": 5,
         "banana": 2,
         "cherry": 10,
         "guava": 15
        }
       }
      ],
      "status": 0
     }
    ]
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部