MongoDB - 在非规范化模型中查询

时间:2015-06-21 16:56:33

标签: mongodb

我必须展示使用标准化模型(在本例中)的优势,在MongoDB中有两个集合:第一个用于教练,第二个用于团队。 所以,我使用非规范化模型来显示差异。 例如,如果我想要更新Barcellona的palmarès(从23 Liga到24 Liga),使用非规范化模型,我必须找到所有执教Barcellona的教练,然后我必须更新每个教练内的palmarès。显然它太贵了。我想做这个例子。

我有他们执教的足球教练和球队的非正规化模型。 这是一个例子:

     "_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

如何使用非规范化模型更新palmarès?

1 个答案:

答案 0 :(得分:1)

在更新中使用 $ positional operator ,这标识要更新的数组中的元素,而不显式指定数组中元素的位置。由于位置 $ 运算符充当与查询文档匹配的第一个元素的占位符,因此数组字段必须显示为查询文档的一部分,因此您需要{{1}查询中的数组字段:

coached_Team