MongoDB DEV · 题目 214 · 多选

Consider a one-to-many relationship observed between a county and the cities in the county. Which of the following are valid ways to model this relationship in MongoDB? Check all that apply:

题目选项

  1. A.
    Create a collection to contain all the cities and all the counties. Then there is no need to add additional references.
  2. B.
    Create a collection for the counties and a collection for the cities. Then in each city document, reference the county the city belongs to.
  3. C.
    Create a collection for the counties. Then in the county document, embed all the cities as an array of cities.

答案置信度:高

正确答案:B、C

解析语言:中文

一对多有两种经典且都有效的建模方式:把“多”的一端内嵌成“一”的一端的数组(county 文档里放城市数组,A),或把关系存在“多”的一端(每个 city 文档保存所属 county 的引用,C),具体选哪种取决于城市数量是否有界、访问模式与更新频率。B 把城市和县混在同一个集合里又不加任何引用字段,等于放弃了关系的表达,不是有效建模。

所在套题

此题在题库中有 3 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗