gpt4 book ai didi

mongodb - 如何在 MongoDB 中处理大数据集

转载 作者:可可西里 更新时间:2023-11-01 09:43:18 25 4
gpt4 key购买 nike

我需要帮助来决定哪种模式类型更适合我的 mongodb 集合。

假设我想存储一个人拥有的东西的列表。会有相对人数,但一个人可以拥有很多东西。假设人以数百为单位,但一个人拥有的东西以数十万为单位。

我可以想到两种选择:

选项 1:

    [{
id: 1,
name: "Tom",
things: [
{
name: 'red tie',
weight: 0.3,
value: 5
},
{
name: 'carpet',
weight: 15,
value: 700
} //... and 300'000 other things
]
},
{
id: 2,
name: "Rob",
things: [
{
name: 'can of olives',
weight: 0.4,
value: 2
},
{
name: 'Porsche',
weight: 1500,
value: 40000
}// and 170'000 other things
]
}//and 214 oher people]
]

选项 2:

[
{
name: 'red tie',
weight: 0.3,
value: 5,
owner: {
name: 'Tom',
id: 1
}
},
{
name: 'carpet',
weight: 15,
value: 700,
owner: {
name: 'Tom',
id: 1
}
},
{
name: 'can of olives',
weight: 0.4,
value: 2,
owner: {
name: 'Rob',
id: 2
}
},
{
name: 'Porsche',
weight: 1500,
value: 40000,
owner: {
name: 'Rob',
id: 2
}
}// and 20'000'000 other things
];
  1. 我只会在单个请求中向一个所有者请求东西,绝不会向多个所有者请求东西。
  2. 我需要一个分页来返回列表,所以...
  3. ...需要根据参数之一排序

据我了解,第一点表明使用选项 1(仅查询数百个文档而不是数百万个文档)会更有效率,但是使用选项 2(限制,跳过)时,第 2 点和第 3 点的处理要容易得多和排序方法,而不是 $slice 投影和聚合框架)。

谁能告诉我哪种方式更合适?又或者我出了什么问题,还有更好的解决方案?

最佳答案

  1. I will only ask for things from one owner in a single request and never ask for things from multiple owners.
  2. I will need a pagination for the returned list of things so...
  3. things will need to be sorted by one of the parameters

通过创建一个集合,其中每个项目都是一个单独的文档,您的要求 2 和 3 将得到更好的满足。对于数组,您将不得不使用聚合框架 $unwind 该数组,这可能会变得非常慢。通过在所述集合的 owner.nameowner.id 字段上创建索引,可以轻松优化您的第一个要求,具体取决于您用于查询的内容。

此外,MongoDB 不能很好地处理不断增长的文档。为了阻止用户创建无限增长的文档,MongoDB 对每个文档有 16MB 的限制。当您的每个项目都是几百个字节时,数十万个数组条目将超过该限制。

关于mongodb - 如何在 MongoDB 中处理大数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794173/

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