Mongodb 的一些概念

基本概念,还是对照常用的关系型数据库,这样更容易理解一些

6V7h3n.png

collection

mongodb是通过一个个collection来做数据存储的,可以类比关系型数据库的表。

shard_key (分片键)

mongodb是通过分片键来对collection进行分区的,也就是通过分片键来决定一个document如何分布式存入collection中。分片键是每个存放在collection中的document都持续拥有的不可缺少的一个字段或多个字段的组合。 分片键有下面几个要求:

  • 每个document都必须拥有,不可缺少。
  • 已经分片的数据,分片键不可更改。
  • 分片键必须加上索引。
  • 分片键的选择对分片的性能、效率和可扩展性都有着重要影响。分片键和索引也会影响集群的分片策略。

分片键索引

分片键必须有索引,索引可以是分片键上的索引,当分片键是索引前缀时,也可以是复合索引。 注意:分片键索引必须是横向(正向),比如用id做索引时定义为key{id:1} 参考:https://docs.mongodb.com/manual/core/sharding-shard-key/#sharding-shard-key-indexes

shard key在sharding搭建完毕后是不能修改的,一个collection上只能有一个shard key。
shard key上必须有索引(可以是以shard key开头的联合索引),如果没有mongodb会为shard key创建索引。如果是已经存在的collection那么必须手动为shard key创建索引。

MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.

mongodb offical doc https://docs.mongodb.com/manual/indexes/

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~