2015-06-21 64 views
0

我必須展示使用規範化模型(在這種情況下)的優勢,MongoDB中有兩個集合:第一個用於教練,第二個用於團隊。 因此,我使用非規範化模型來顯示差異。 例如,如果我想更新Barcellona的手掌(從23個西甲到24個西甲),並且使用非規範化模型,我必須找到所有執教Barcellona的教練,然後我必須更新每個教練內的手掌。顯然它太昂貴了。我想要做這個例子。MongoDB - 非規範化模型中的查詢

我有足球教練和他們執教過的球隊的非規範化模型。 下面是一個例子:

 "_id" : "LEMG_1970", 
    "name" : "Luis", 
    "surname" : "Enrique Martinez Garcia", 
    "age" : 45, 
    "date_Of_birth" : { 
      "day" : 8, 
      "month" : 5, 
      "year" : 1970 
    }, 
    "place_Of_birth" : "Gijòn", 
    "nationality" : "Spanish", 
    "preferred_formation" : "4-3-3 off", 
    "coached_Team" : [ 
      { 


"_id" : "Bar.43", 
    "official_name" : "Futbol Club Barcelona" 
    "common_name" : "Barcellona", 
    "country" : "Spain", 
    "started_by" : { 
      "day" : 28, 
      "month" : 11, 
      "year" : 1899 
    }, 
    "championship" : "La Liga", 
    "stadium" : { 
      "name" : "Camp Nou", 
      "capacity" : 99354 
    }, 
    "palmarès" : { 
      "La Liga" : 23, 
      "Copa del Rey" : 27, 
      "Supercopa de Espana" : 11, 
      "UEFA Champions League" : 4, 
      "UEFA Cup Winners Cup" : 4, 
      "UEFA Super Cup" : 4, 
      "FIFA Club World cup" : 2 
    }, 
    "average age" : 26.9, 
    "squad value(in mln)" : 591.5, 
    "foreigners" : 13, 
    "uniform" : [ 
      "blue", 
      "dark red" 
    ], 
        "in_charge" : { 
          "from" : { 
            "day" : 1, 
            "month" : 7, 
            "year" : 2014 
          } 
        }, 
        "matches" : 59 
      }, 
      { 

      { 
     "_id" : "Rom.01", 
    "official_name" : "Associazione Sportiva Roma SpA", 
    "common_name" : "Roma", 
    "country" : "Italy", 
    "started_by" : { 
      "day" : 22, 
      "month" : 6, 
      "year" : 1927 
    }, 
    "championship" : "Serie A", 
    "stadium" : { 
      "name" : "Olimpico di Roma", 
      "capacity" : 73261 
    }, 
    "palmarès" : { 
      "Serie A" : 3, 
      "Coppa Italia" : 9, 
      "Supercoppa Italiana" : 2, 
      "Serie B" : 1 
    }, 
    "average age" : 28.3, 
    "squad value(in mln)" : 253.7, 
    "foreigners" : 22, 
    "uniform" : [ 
      "red", 
      "yellow" 
    ], 
        "in_charge" : { 
          "from" : { 
            "day" : 7, 
            "month" : 6, 
            "year" : 2011 
          }, 
          "to" : { 
            "day" : 10, 
            "month" : 5, 
            "year" : 2012 
          } 
        }, 
        "matches" : 41 
      } 
    ] 

正如你可以看到關於球隊的信息是到教練的文檔。現在,我想更新Barcellona的手掌。我想這個查詢,但我得到了一個錯誤:

db.coach.update({_id:"LEMG_1970"}, {$set:{"coached_Team.palmarès.La Liga":24}}) 

這是忠告:

"code" : 16837, 
      "errmsg" : "cannot use the part (coached_Team of coached_Team.palmarès.La Liga) to traverse the element 

我能做些什麼來更新與非規格化模式帕爾馬里斯?

回答

1

在更新中使用$ positional operator這標識了數組中要更新的元素,而不顯式指定數組中元素的位置。由於位置$運營商充當了查詢文檔相匹配的第一個元素的佔位符,該數組字段必須出現在查詢文檔的一部分,因此,您需要在您的查詢coached_Team陣列領域:

var query = { 
     "_id" : "LEMG_1970", 
     "coached_Team._id" : "Bar.43" 
    }, 
    update = { 
     "$set": { 
      "coached_Team.$.palmarès.La Liga": 24 
     } 
    }; 

db.coach.update(query, update); 
+1

非常好,很棒的提示。 – DistribuzioneGaussiana