【发布时间】:2015-03-29 12:06:35
【问题描述】:
我正在使用 MongoDB。我知道 MongoDB 不是关系型的,但信息有时是关系型的。那么,引用这些类型的关系以减轻数据库负载并最大化查询速度的最有效方法是什么?
例子:
* Tinder 风格的“匹配”*
Users 集合中有许多用户。它们相互匹配。
所以我在想:
Document 1:
{
_id: "d3fg45wr4f343",
firstName: "Bob",
lastName: "Lee",
matches: [
"ferh823u9WURF",
"8Y283DUFH3FI2",
"KJSDH298U2F8",
"shdfy2988U2Ywf"
]
}
文档 2:
{
_id: "d3fg45wr4f343",
firstName: "Cindy",
lastName: "Doe",
matches: [
"d3fg45wr4f343"
]
}
如果有 10,000 名用户,并且您在 Bob 的个人资料页面上并且您想显示他所有匹配项的名字,这是否可行?
有没有更好的替代结构?
* 在线论坛 *
我想你可以有以下收藏:
用户
主题
用户收藏:
{
_id: "d3fg45wr4f343",
userName: "aircon",
avatar: "234232.jpg"
}
{
_id: "23qdf3a3fq3fq3",
userName: "spider",
avatar: "986754.jpg"
}
主题集合版本 1
主题集合中的一个示例文档:
{
title: "A spider just popped out of the AC",
dateTimeSubmitted: 201408201200,
category: 5,
posts: [
{
message: "I'm going to use a gun.",
dateTimeSubmitted: 201408201200,
author: "d3fg45wr4f343"
},
{
message: "I don't think this would work.",
dateTimeSubmitted: 201408201201,
author: "23qdf3a3fq3fq3"
},
{
message: "It will totally work.",
dateTimeSubmitted: 201408201202,
author: "d3fg45wr4f343"
},
{
message: "ur dumb",
dateTimeSubmitted: 201408201203,
author: "23qdf3a3fq3fq3"
}
]
}
主题集合第 2 版
主题集合中的一个示例文档。作者的头像和用户名现在嵌入到文档中。我知道:
这不是 DRY。
如果作者更改了他们的头像和用户名,则需要在主题集合和其中的所有帖子文档中更新这些更改。
但是,每次在客户端上查看此线程时,它都会使系统免于通过作者 ID 查询所有头像和用户名。
{
title: "A spider just popped out of the AC",
dateTimeSubmitted: 201408201200,
category: 5,
posts: [
{
message: "I'm going to use a gun.",
dateTimeSubmitted: 201408201200,
author: "d3fg45wr4f343",
userName: "aircon",
avatar: "234232.jpg"
},
{
message: "I don't think this would work.",
dateTimeSubmitted: 201408201201,
author: "23qdf3a3fq3fq3",
userName: "spider",
avatar: "986754.jpg"
},
{
message: "It will totally work.",
dateTimeSubmitted: 201408201202,
author: "d3fg45wr4f343",
userName: "aircon",
avatar: "234232.jpg"
},
{
message: "ur dumb",
dateTimeSubmitted: 201408201203,
author: "23qdf3a3fq3fq3",
userName: "spider",
avatar: "986754.jpg"
}
]
}
所以是的,我不确定哪个最好......
【问题讨论】:
标签: mongodb