【问题标题】:How to create a large number of documents with embedded documents in MongoDB?如何在 MongoDB 中创建大量嵌入文档的文档?
【发布时间】:2021-09-03 11:31:08
【问题描述】:

我在这里查看示例:

   _id: "joe",
   name: "Joe Bookreader"
}
// address documents
{
   patron_id: "joe", // reference to patron document
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
}
{
   patron_id: "joe",
   street: "1 Some Other Street",
   city: "Boston",
   state: "MA",
   zip: "12345"
}

他们在个人文档中嵌入地址文档的位置。如果您的个人文档中有一个项目,该示例是有意义的,但是如果您有一个包含 100 个项目和每个项目的地址的个人文档,您将如何扩展它?有没有办法创建架构并上传数据,还是必须手动为每个人嵌入地址文档?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您应该阅读他们文档中的数据模型设计部分。对于此模式,Relationships with Document References 将匹配。

    简而言之,您的 User 文档将有一个数组,其中包含引用的 Address 文档的 ID。但是地址存储在不同的集合中。您需要第二个查询来选择它们。

    文档中的示例:

    {
       name: "O'Reilly Media",
       founded: 1980,
       location: "CA",
       books: [123456789, 234567890, ...]
    }
    {
        _id: 123456789,
        title: "MongoDB: The Definitive Guide",
        author: [ "Kristina Chodorow", "Mike Dirolf" ],
        published_date: ISODate("2010-09-24"),
        pages: 216,
        language: "English"
    }
    {
       _id: 234567890,
       title: "50 Tips and Tricks for MongoDB Developer",
       author: "Kristina Chodorow",
       published_date: ISODate("2011-05-06"),
       pages: 68,
       language: "English"
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 2020-12-27
      • 2012-04-26
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多