2017-12-20 19 views
1

mgo를 사용하여 문서의 배열 내부에서 개체를 업데이트하려고합니다.내 쿼리가 배열 내부의 개체를 업데이트하지 않는 이유는 무엇입니까?

{ 
"_id": 2, 
"status": 0, 
"details": [{ 
    "id": 2, 
    "category": "A", 
    "obj": { 
     "apple": 5, 
     "banana": 2, 
     "cherry": 10 
    }, 
    "members": [{ 
      "id": 3, 
      "category": "A", 
      "obj": { 
       "apple": 5, 
       "banana": 2, 
       "cherry": 10 
      } 
     }, 
     { 
      "id": 4, 
      "category": "A", 
      "obj": { 
       "apple": 5, 
       "banana": 2, 
       "cherry": 10 
      } 
     } 
    ] 
}] 
} 

에 Query1 : 다음 쿼리

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

이 쿼리는 둘 다 어떤을 생산하지 않습니다를 사용하여 다른 속성 "guava": 15을 추가하려는 경우 가 처음 details > obj를 업데이트하려고 개체의 구조는 다음과 같습니다 오류 또는 문서 업데이트 중. 아무도 그것을 어떻게 성취 할 수 있는지 말해 줄 수 있습니까?

참고 : 나는 검색했지만 원하는 내용과 관련이 없습니다.

QUERY2 : 나는 또한 내가 details > obj을 위해 할 애 쓰고 details > members > obj 같은 방법으로 업데이트해야합니다. details > members > obj에 대해서도 어떻게 같은 결과를 얻을 수 있는지 알려주세요.

나는 이것을 알아 내는데 몇 시간을 허비했으나 아무것도 해결하지 못했습니다. 나는 누군가가 나를 인도 할 수 있다면 감사 할 것이다.

답변

0

간단히 말해 회원을 삭제합니다.

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\n", 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\n", string(data)) 
} 

결과 : 또한

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 
} 
] 
+0

참조 : https://docs.mongodb.com/manual/reference/operator/update/positional/#up._S_ 응답에 대한 –

+0

감사합니다,하지만 같은 일을하고 있어요. 쿼리에서 "문서"(나는 컬렉션 이름과 같은 개체 이름 즉 개체 이름으로 나타 내기 위해 여기에서 사용하고 있음)을 제거했지만 개체를 ​​업데이트하지 않았습니다. – MKB

+0

아마도 뭔가 잘못되었으므로 pls는 col.update()를 호출 할 때 오류 값을 출력합니다. –