【发布时间】:2021-12-26 10:22:52
【问题描述】:
他在那里,我曾经与 Relational DBS 合作,但现在正在尝试实现电子商务商店并使用 mongodb。
我需要产品、子产品和描述(多语言);
我更喜欢用 3 个集合分隔所有内容(这可能不是一个好主意,因为 mongo 对一个实体使用 1 个集合,在我的示例中,1 个实体使用 3 个集合)
"content": [{
pid: 1,
lang: "ru",
title: "Привет"
},
{
pid: 1,
lang: "en",
title: "Hello"
},
{
pid: 2,
lang: "ru",
title: "Пока"
},
{
pid: 2,
lang: "en",
title: "Bye"
}
],
"products": [{
"_id": 1,
"item": "almonds",
"price": 12,
},
{
"_id": 2,
"item": "pecans",
"price": 20,
},
],
"sub": [{
"_id": 11,
"pid": 1,
"features": {
"color": ["red"],
"size": 42
},
"qt": 5
},
{
"_id": 12,
"pid": 1,
"features": {
"color": ["red"],
"size": 43
},
"qt": 2
},
{
"_id": 13,
"pid": 1,
"features": {
"color": ["yellow"],
"size": 44
},
"qt": 3
},
{
"_id": 21,
"pid": 2,
"features": {
"color": ["yellow"],
"size": 41
},
"qt": 6
},
{
"_id": 22,
"pid": 2,
"features": {
"color": ["red"],
"size": 47
},
"qt": 10
}
]
产品应该有子产品才能使用过滤器,例如,当我想过滤项目时,我会在子产品集合中查找所有黄色 t-short,例如尺寸为 44,然后我只需 $group主productId的项目和主产品一起制作$lookup并退货。
另外,为了接收带有描述的主要产品,我应该使用content 收集$lookup。
这是个好主意还是我应该为product 和content 使用1 个集合?
喜欢:
"products": [{
"_id": 1,
"item": "almonds",
"price": 12,
"content": [{
lang: "ru",
title: "Привет"
},
{
lang: "en",
title: "Hello"
},
},
]
也许我应该在主要产品中也包含子项目,例如:
"products": [{
"_id": 1,
"item": "almonds",
"price": 12,
"content": [{
lang: "ru",
title: "Привет"
},
{
lang: "en",
title: "Hello"
},
},
"sub": [{
"features": {
"color": ["red"],
"size": 42
},
"qt": 5
},
{
"features": {
"color": ["red"],
"size": 43
},
"qt": 2
},
]
]
主要问题是比较所有内容而不关心集合的大小是个好主意吗?如果是这样,我应该如何对嵌套文档('sub-products')进行过滤(以前的'sub-products'集合就像一个普通集合,我可以进行聚合以便按颜色查找所有项目,例如:@987654332 @嵌套文档如何管理,又不会被压垮操作?
【问题讨论】:
-
MongoDB 数据建模是一个涉及多个方面的过程 - 包括实体之间的关系、数据的大小、每条记录中的数据大小、您要执行的重要 CRUD / 查询操作等。 MongoDB文档的最大大小为 16 MB,这允许文档中的非规范化数据。另请参阅:Data Modeling Introduction.
标签: javascript database mongodb database-design architecture