gpt4 book ai didi

mongodb - 了解 MongoDB 引用

转载 作者:行者123 更新时间:2023-12-02 17:28:41 25 4
gpt4 key购买 nike

我阅读了 MongoDB 中的数据模型并来到 one to many relationship:

在下面的示例中,这是否意味着书籍文档应该在集合 books 中,MongoDB 将以某种方式将 ID 与数组 books 匹配的文档链接到出版商?还是这种联系只存在于架构师的头脑中?

{
name: "O'Reilly Media",
founded: 1980,
location: "CA",
books: [12346789, 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"
}

最佳答案

MongoDB 不会以某种方式将文档与 ids 相匹配的数组书籍链接到 publisher。所以是的,链接只存在于架构师的头脑中

这篇文章只是试图解释如何在 MongoDB 中的文档之间建立一对多关系。

一对多关系在MongoDB中也被进一步分类one to fewone to squillions。如果场景是one to few,可以使用这样的方法:

{
name: "O'Reilly Media",
founded: 1980,
location: "CA",
books: [12346789, 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"
}

但实际上,出版商可以出版数百万本书。因此,在那种情况下,这将是 one to squillions 的一个例子。现在存储数百万个 book_ids 可能会使 MongoDB 文档超过 16MB 的大小,这是 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"
}

关于mongodb - 了解 MongoDB 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36458449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com