列表字段可以作为MongoDB中的分片键吗? [英] Can a list field be a shard key in MongoDB?
本文介绍了列表字段可以作为MongoDB中的分片键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一些看起来像这样的数据:
Have some data that looks like this:
widget:
{
categories: ['hair', 'nails', 'dress']
colors: ['red', 'white']
}
需要像这样查询数据:
SELECT * FROM widget_table WHERE categories == 'hair' AND colors == 'red'
想将此数据放入MongoDB分片群集中.但是,似乎理想的分片键不会是列表字段.在这种情况下,这是不可能的,因为所有字段都是列表字段.
Would like to put this data into a MongoDB sharded cluster. However, it seems like an ideal shard key would not be a list field. In this case, that is not possible because all of the fields are list fields.
- 是否可以将列表字段(例如字段
categories
)用作MongoDB中的分片键? - 如果是,我应该注意/要注意些什么?
- Is it possible to use a list field, such as the field
categories
as the shard key in MongoDB? - If so, what things should I look out for / be aware of?
非常感谢!
推荐答案
基于一些反馈,我似乎断言无法使用列表字段作为分片键进行分片,我想说明如何使用MongoDB的限制来分割此用例:
Based on some of the feed back I am getting that seems to assert that it is not possible to shard using a list field as a shard key, I wanted to illustrate how this use case could be sharded using the limitations of MongoDB:
widget:
{
primary_key: '2389sdjsdafnlfda'
categories: ['hair', 'nails', 'dress']
colors: ['red', 'white']
#All the other fields in the document that don't need to be queried upon:
...
...
}
数据层根据为分片键选择的字段中的元素数将对象分为多个指针对象:
widget_pointer:
{
primary_key: '2389sdjsdafnlfda'
categories: 'hair',
colors: ['red', 'white']
}
widget_pointer:
{
primary_key: '2389sdjsdafnlfda'
categories: 'nails',
colors: ['red', 'white']
}
widget_pointer:
{
primary_key: '2389sdjsdafnlfda'
categories: 'dress',
colors: ['red', 'white']
}
说明:
- 字段
categories
现在可以成为MongoDB中的分片键. - 原始对象现在将存储在键值存储中.对MongoDB中的数据的查询将返回一个指针对象,该指针对象将用于从键值存储中获取该对象.
- 对MongoDB数据的查询将仅命中一个分片.
- 对MongoDB数据的插入将与列表中的元素一样多地命中碎片,在大多数情况下,仅会影响碎片总数的一小部分.
- The field
categories
can now be the shard key in MongoDB. - The original object will now be stored in a key-value store. Queries against the data in MongoDB will return a pointer object that will be used to get the object from the key-value store.
- Queries on the MongoDB data will hit only one shard.
- Insertions on the MongoDB data will hit as many shards as there are elements in the list, in most cases, only a small subset of the total number of shards will be affected.
Explanation:
这篇关于列表字段可以作为MongoDB中的分片键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文