原文地址https://docs.mongodb.org/manual/reference/limits/


BSON文档

BSON 文档大小


    BSON 最大是16M

    限制BSON文档大小的原因是:

        1.文档内存占用

        2.网络传输时带宽的占用

        3.要存储大文档请使用GridFS


BSON 内嵌文档的深度


    MongoDB 支持不超过100层级的嵌套格式 BSON 文档.


命名空间


命名空间长度

    MongoDB 集合名称的最大长度不超过120字节

    文档名称 = 数据库名 + 集合名 + 每个名称的分隔符(分隔符是‘.’)


命名空间的数量

Changed in version 3.0.


    对 MMAPv1 来说命名空间的数量受限于命名空间文件的大小除以628。一个16M大小的命名空间文件可以支持接近24000个命名空间。每个集合和索引都占一个命名空间。WiredTiger 存储引擎不受这个限制。

命名空间文件大小

Changed in version 3.0.


对 MMAPv1储存引擎来说, 命名空间文件的大小不能超过2047M。默认的命名空间文件大小是16M。

可以通过 选项 nsSize 来设置。WiredTiger 存储引擎不受这个限制。


索引


索引长度

一个索引条目大小必须小于1024 字节.


单个集合的索引个数限制

    单个集合不能超过64个索引。


索引名称长度

    全限定的索引名称 (i.e. ..$), 不能超过128个字符。


组合索引包含字段个数限制

    一个组合索引不能超过31个字段


查询不能同时使用文本索引和地理空间索引


多键索引

    一个多键索引不支持 covered 查询。


数据


有上限集合的最大文档数

Changed in version 2.4.

    在创建一个有文档数目上限的集合时如果不指定参数max, 则该集合没有文档数目限制。如果指定max参数则不能超过2的32次方。


数据库大小

     MMAPv1 存储引擎限制每个数据库不能超过16000 个数据文件. 这意味着单个 MMAPv1 引擎的数据库的数据不能超过32TB。 设置参数 storage.mmapv1.smallFiles 可以减小这个xian值到 8TB。


数据大小

Changed in version 3.0.


    使用 MMAPv1 存储引擎, 单个mongod 实例管理的数据集不能超过操作系统提供的虚拟内存的最大空间。

    

Virtual Memory Limitations
操作系统 Journaled Not Journaled
linxu
64T
128T
Windows Server 2012 R2 and Windows 8.1 64T 128T
Windows othre 4T 8T

WiredTiger 存储引擎没有这个限制。


数据库中集合数据限制


Changed in version 3.0.


对 MMAPv1存储引擎来说。 一个数据库中集合数目的限制取决于命名空间文件的大小和该数据库中所有集合索引的数目.


WiredTiger 存储引擎没有这个限制。


复制集


复制集中成员数目的限制

Changed in version 3.0.0.

    复制集中成员个数不能超过50个


复制集中可以投票成员数目限制


    一个复制集中参与投票的成员不能超过7个。


自动创建的Oplog 集合大小

Changed in version 2.6.

    如果你没有指定oplog的大小oplogSizeMB 或者 --oplogSize。MongoDB将自动创建一个大小不超过50G的opLog集合。


分片集群


分片集群操作限制


    一些操作在分片集群环境下是不可用的可以通过mapReduce 或者聚合替代。


    db.eval()操作多分片后的集合是不适用的。你可以对分片集群中一个不分片的集合使用 db.eval()操作。


$where does not permit references to the db object from the $where function. This is uncommon in un-sharded collections.


The $isolated update modifier does not work in sharded environments.


$snapshot queries do not work in sharded environments.


The geoSearch command is not supported in sharded environments.


Covered Queries in Sharded Clusters

An index cannot cover a query on a sharded collection when run against a mongos if the index does not contain the shard key, with the following exception for the _id index: If a query on a sharded collection only specifies a condition on the _id field and returns only the _id field, the _id index can cover the query when run against a mongos even if the _id field is not the shard key.


Changed in version 3.0: In previous versions, an index cannot cover a query on a sharded collection when run against a mongos.


对已经存在的集合进去切片

    对已经含有数据的集合如果集合大小不超过256G则MongoDB支持对该集合进去分片。 MongoDB 可能支持对不超过400G的集合进行分片这依赖于文档的分布情况。具体可以参考下表。

Shard Key Size 512 bytes 200 bytes
Number of Splits 31,558 85,196
Max Collection Size (1 MB Chunk Size) 31 GB 83 GB
Max Collection Size (64 MB Chunk Size) 1.9 TB 5.3 TB

The data in this chart reflects documents with no data other than the shard key values and therefore represents the smallest possible data size that could reach this limit.


重点

    成功启用分片后的集合大小是不固定的。

    分片集合中单个文档修改操作限制

    一个启用分片的集合的所有的修改和删除操作 that specify the justOne or multi: false option must include the shard key or the _id field in the query specification. update() and remove() operations specifying justOne or multi: false in a sharded collection without the shard key or the _id field return an error.


分片集合中唯一索引限制

MongoDB does not support unique indexes across shards, except when the unique index contains the full shard key as a prefix of the index. In these situations MongoDB will enforce uniqueness across the full key, not a single field.


SEE

Enforce Unique Keys for Sharded Collections for an alternate approach.

Maximum Number of Documents Per Chunk to Migrate

MongoDB cannot move a chunk if the number of documents in the chunk exceeds either 250000 documents or 1.3 times the number of average sized documents that the maximum chunk size can hold.


Shard Key Limitations

Shard Key Size

A shard key cannot exceed 512 bytes.


Shard Key Index Type

A shard key index can be an ascending index on the shard key, a compound index that start with the shard key and specify ascending order for the shard key, or a hashed index.


A shard key index cannot be an index that specifies a multikey index, a text index or a geospatial index on the shard key fields.


Shard Key is Immutable

You cannot change a shard key after sharding the collection. If you must change a shard key:


Dump all data from MongoDB into an external format.

Drop the original sharded collection.

Configure sharding using the new shard key.

Pre-split the shard key range to ensure initial even distribution.

Restore the dumped data into MongoDB.

Shard Key Value in a Document is Immutable

After you insert a document into a sharded collection, you cannot change the document’s value for the field or fields that comprise the shard key. The update() operation will not modify the value of a shard key in an existing document.


Monotonically Increasing Shard Keys Can Limit Insert Throughput

For clusters with high insert volumes, a shard keys with monotonically increasing and decreasing keys can affect insert throughput. If your shard key is the _id field, be aware that the default values of the _id fields are ObjectIds which have generally increasing values.


When inserting documents with monotonically increasing shard keys, all inserts belong to the same chunk on a single shard. The system will eventually divide the chunk range that receives all write operations and migrate its contents to distribute data more evenly. However, at any moment the cluster can direct insert operations only to a single shard, which creates an insert throughput bottleneck.


If the operations on the cluster are predominately read operations and updates, this limitation may not affect the cluster.


To avoid this constraint, use a hashed shard key or select a field that does not increase or decrease monotonically.


Changed in version 2.4: Hashed shard keys and hashed indexes store hashes of keys with ascending values.


Operations


Sort Operations

If MongoDB cannot use an index to get documents in the requested sort order, the combined size of all documents in the sort operation, plus a small overhead, must be less than 32 megabytes.


Aggregation Pipeline Operation

Changed in version 2.6.


Pipeline stages have a limit of 100 megabytes of RAM. If a stage exceeds this limit, MongoDB will produce an error. To allow for the handling of large datasets, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.


SEE ALSO

$sort and Memory Restrictions and $group Operator and Memory.

2d Geospatial queries cannot use the $or operator

SEE

$or and 2d Index Internals.

Area of GeoJSON Polygons

For $geoIntersects or $geoWithin, if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the $geometry expression; otherwise, $geoIntersects or $geoWithin queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere, $geoIntersects or $geoWithin queries for the complementary geometry.


Write Command Operation Limit Size

Write commands can accept no more than 1000 operations. The Bulk() operations in the mongo shell and comparable methods in the drivers do not have this limit.


Naming Restrictions


Database Name Case Sensitivity

MongoDB does not permit database names that differ only by the case of the characters.


Restrictions on Database Names for Windows

Changed in version 2.2: Restrictions on Database Names for Windows.


For MongoDB deployments running on Windows, MongoDB will not permit database names that include any of the following characters:


/\. "$*<>:|?

Also, database names cannot contain the null character.


Restrictions on Database Names for Unix and Linux Systems

For MongoDB deployments running on Unix and Linux systems, MongoDB will not permit database names that include any of the following characters:


/\. "$

Also, database names cannot contain the null character.


Length of Database Names

Database names cannot be empty and must have fewer than 64 characters.


Restriction on Collection Names

New in version 2.2.


Collection names should begin with an underscore or a letter character, and cannot:


contain the $.

be an empty string (e.g. "").

contain the null character.

begin with the system. prefix. (Reserved for internal use.)

In the mongo shell, use db.getCollection() to specify collection names that might interact with the shell or are not valid JavaScript.


Restrictions on Field Names

Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $). See Dollar Sign Operator Escaping for an alternate approach.


←