mongodb - 编写集合模式以映射另一个集合的最佳方法是什么?
问题描述
在 mongodb 中,我有很多集合,如下所示
boys_fashion
girls_fashion
gents_fashion
ladies_fashion
girls_accessories
gents_accessories
ladies_accessories
基于某些字段,我需要使用不同的集合。因此,为此我想创建一个将映射到特定集合的集合。下面是我为此创建的集合。
{
"type" : "Fashion",
"gender" : "Gents",
"kids" : "true",
"collection" : "boys_fashion"
}
{
"type" : "Accessories",
"gender" : "Ladies",
"kids" : "false",
"collection" : "ladies_accessories"
}
{
"type" : "Fashion",
"gender" : "Gents",
"kids" : "false",
"collection" : "gents_fashion"
}
{
"type" : "Accessories",
"gender" : "Ladies",
"kids" : "true",
"collection" : "girls_accessories"
}
{
"type" : "Accessories",
"gender" : "Gents",
"kids" : "true",
"collection" : "gents_accessories"
}
{
"type" : "Accessories",
"gender" : "Gents",
"kids" : "false",
"collection" : "gents_accessories"
}
这是正确的方法吗?或者请给我一些其他的建议
如果我像下面这样存储(上面的选项类似于 RDBMS。因为它的 mongo 我想我用这种方式)。如何编写查询以获取集合?
{
"fashion" : {
"gents" : {
"true" : "boys_fashion",
"false" : "gents_fashion"
}
},
"accessories" : {
"ladies" : {
"true" : "girls_accessories",
"false" : "ladies_accessories"
}
}
}
解决方案
假设:
- 之前有一个集合,您将它们拆分为多个集合,因为它们变得越来越大,您想在不共享的情况下解决它。
我什至不会为以下数据创建一个集合。该数据是静态参考数据,将充当路由器。启动时,应用程序加载此数据并创建路由器。
{
"type" : "Fashion",
"gender" : "Gents",
"kids" : "true",
"collection" : "boys_fashion"
}
{
"type" : "Accessories",
"gender" : "Ladies",
"kids" : "false",
"collection" : "ladies_accessories"
}
...
通过该静态配置文件创建一种路由器是什么意思?假设您收到查询女婴时尚物品。这个路由器会告诉你,
hey you need to search girls_accessories collection
. 然后您将查询发送到girls_accessories
集合并返回结果。让我们再举一个例子,您收到一个关于女性时尚物品的查询。这个路由器会告诉你
hey you need to search, ladies_accessories and girls_accessories
。您将查询发送到两个集合并组合结果并将其发送回。
结论
如果我的假设是正确的,那么您不需要集合来存储参考数据。您所做的是通过将数据拆分到不同的集合中进行手动分片,参考数据将充当路由器来告诉您在哪里搜索和组合
根据评论更新
- 查询不涉及多个集合
- 管理员可以添加新集合,应用程序应该在不修改代码的情况下查询它。
解决方案 1
- 您也为参考数据创建了一个集合
- 这样做的缺点是每个查询都涉及对数据库的两次调用。首先获取静态数据,然后获取
data
集合
解决方案 2
- 您也为参考数据创建了一个集合
@Cacheable
您还可以在用于返回此数据的方法的基础上构建一个 Dao 。- 您还可以在 Dao 中添加一个方法来清除缓存,
@CacheEvict
并有一个类似的休息端点/refresh-static-data
将调用此方法` - 此方法的缺点是每当管理员添加新集合时,您都需要调用端点。
解决方案 3
- 与解决方案 2 相同,但不是使用端点来清除缓存,而是将其与调度程序结合使用
@Scheduled(fixedRate = ONE_DAY)
@CacheEvict(value = { CACHE_NAME })
public void clearCache() {
}
- 此解决方案的缺点是您必须想出一个
fixedRate
您的业务可以接受的时间段
推荐阅读
- javascript - 应用更新后 xxx.chunk.js 返回 422 (Unprocessable Entity)
- javascript - 在 javascript 中解析服务器端文本文件的日期,然后在文件的另一行显示文本
- asp.net-core - .Net Core SignalR 服务器到服务器重新连接
- sql-server-2012 - SQL中的数据转换为十六进制
- c - 为什么 IRQF_SAMPLE_RANDOM 标志已被删除?
- python - NameError:名称“training_set”未定义
- google-sheets - 有没有办法在 Google 表格中显示嵌入的推文?
- android - 从嵌套的 JSON 颤振中获取值和键
- sql-server - SSAS MDX 计算成员:3 个月移动平均线不起作用
- python - Template Inheritance(Jinja2 Templating) - 我的代码中的缺陷或对概念的理解