【问题标题】:How to model one-to-many relationships across collections with _id in mongodb?如何在mongodb中使用_id建模跨集合的一对多关系?
【发布时间】:2013-07-15 05:11:47
【问题描述】:

mongo db 文档回答了这个问题,但对我来说答案似乎是错误的。从 Model Referenced One-to-Many Relationships Between Documents 上的 mongodb 文档中,他们给出了以下答案:

{
   _id: "oreilly",
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA"
}

{
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: [ "Kristina Chodorow", "Mike Dirolf" ],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English",
    publisher_id: "oreilly"
}

{
    _id: 234567890,
    title: "50 Tips and Tricks for MongoDB Developer",
    author: "Kristina Chodorow",
    published_date: ISODate("2011-05-06"),
    pages: 68,
    language: "English",
    publisher_id: "oreilly"
}

这是文档错误吗?该示例对 _id 的使用似乎与 Mongodb's ObjectId 的文档描述相冲突。

正确的解决方案是让 publisher_id 拥有 12 字节的 BSON 对象 ID?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我认为 _id 的唯一要求是唯一。如果您没有明确给出一个,那么生成的就是文档中描述的对象 id。

    查看来自您链接的同一个地方的报价:

    因为 ObjectIds 很小,很可能是唯一的,而且生成速度很快,如果 _id 字段没有指定,MongoDB 使用 ObjectIds 作为 _id 字段的默认值

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 2017-01-02
      • 2012-01-09
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2016-04-23
      • 2020-05-10
      • 2022-09-23
      相关资源
      最近更新 更多