Mongodb capped collection что это

Обновлено: 18.05.2024

Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations. By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.

Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.

Creating Capped Collection

To create a capped collection, we use the normal createCollection command but with capped option as true and specifying the maximum size of collection in bytes.

In addition to collection size, we can also limit the number of documents in the collection using the max parameter −

If you want to check whether a collection is capped or not, use the following isCapped command −

If there is an existing collection which you are planning to convert to capped, you can do it with the following code −

This code would convert our existing collection posts to a capped collection.

Руководство по MongoDB. Создание коллекций

В данной статье мы рассмотрим процесс создание коллекций в MongoDB.

Метод createCollection()

Для создания коллекций в MongoDB используется следующая команда:

Требования к имени и свойствам следующие:

Параметр Тип Описание
Имя коллекции String Имя создаваемой коллекции
Свойства коллекции Document Определяет такие свойства, как индексация и память.

Поле Тип Описание
capped Boolean Создаёт ограниченную по размеру коллекцию. Если количество элементов в коллекции достигло максимума, то каждая следующая запись будет перезаписывать более старые данные.
autoIndexID Boolean Автоматически создаёт индекс для поля _id field.s
size number Определяет максимальный размер (в байтах) ограниченной (capped) коллекции. Если коллекция ограничена, то необходимо обязательно указать максимальный размер.
max number Определяет максимальное количество документов в ограниченной коллекции.

Пример
Для понимания того, как это работает на практике, рассмотрим простой пример:

Создадим новую БД с именем projectdb:

Создадим в ней ограниченную коллекцию developers, в которой мы сможем хранить только 10 разработчиков:

Для просмотра всех коллекций используется команда:

В MongoDB при вставке документа коллекция создаётся автоматически

Проверим список коллекций ещё раз:

На этом мы заканчиваем изучение способа создания коллекций в MongoDB.
В следующей статье мы рассмотрим способ удаления коллекций.

Querying Capped Collection

By default, a find query on a capped collection will display results in insertion order. But if you want the documents to be retrieved in reverse order, use the sort command as shown in the following code −

There are few other important points regarding capped collections worth knowing −

We cannot delete documents from a capped collection.

There are no default indexes present in a capped collection, not even on _id field.

While inserting a new document, MongoDB does not have to actually look for a place to accommodate new document on the disk. It can blindly insert the new document at the tail of the collection. This makes insert operations in capped collections very fast.

Similarly, while reading documents MongoDB returns the documents in the same order as present on disk. This makes the read operation very fast.

Behavior¶

Insertion Order¶

Capped collections guarantee preservation of the insertion order. As a result, queries do not need an index to return documents in insertion order. Without this indexing overhead, capped collections can support higher insertion throughput.

Automatic Removal of Oldest Documents¶

To make room for new documents, capped collections automatically remove the oldest documents in the collection without requiring scripts or explicit remove operations.

Consider the following potential use cases for capped collections:

  • Store log information generated by high-volume systems. Inserting documents in a capped collection without an index is close to the speed of writing log information directly to a file system. Furthermore, the built-in first-in-first-out property maintains the order of events, while managing storage use.
  • Cache small amounts of data in a capped collections. Since caches are read rather than write heavy, you would either need to ensure that this collection always remains in the working set (i.e. in RAM) or accept some write penalty for the required index or indexes.

For example, the oplog.rs collection that stores a log of the operations in a replica set uses a capped collection. Starting in MongoDB 4.0, unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the majority commit point .

_id Index¶

Capped collections have an _id field and an index on the _id field by default.

Restrictions and Recommendations¶

Updates¶

If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.

Document Size¶

Changed in version 3.2.

If an update or a replacement operation changes the document size, the operation will fail.

Document Deletion¶

You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.

Sharding¶

You cannot shard a capped collection.

Query Efficiency¶

Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. This is similar to using the tail command on a log file.

Aggregation $out ¶

The aggregation pipeline stage $out cannot write results to a capped collection.

Transactions¶

Starting in MongoDB 4.2, you cannot write to capped collections in transactions. Reads from capped collections are still supported in transactions.

Behavior¶

Insertion Order¶

Capped collections guarantee preservation of the insertion order. As a result, queries do not need an index to return documents in insertion order. Without this indexing overhead, capped collections can support higher insertion throughput.

Automatic Removal of Oldest Documents¶

To make room for new documents, capped collections automatically remove the oldest documents in the collection without requiring scripts or explicit remove operations.

Consider the following potential use cases for capped collections:

  • Store log information generated by high-volume systems. Inserting documents in a capped collection without an index is close to the speed of writing log information directly to a file system. Furthermore, the built-in first-in-first-out property maintains the order of events, while managing storage use.
  • Cache small amounts of data in a capped collections. Since caches are read rather than write heavy, you would either need to ensure that this collection always remains in the working set (i.e. in RAM) or accept some write penalty for the required index or indexes.

For example, the oplog.rs collection that stores a log of the operations in a replica set uses a capped collection. Starting in MongoDB 4.0, unlike other capped collections, the oplog can grow past its configured size limit to avoid deleting the majority commit point .

_id Index¶

Capped collections have an _id field and an index on the _id field by default.

Capped Collections¶

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

See createCollection() or create for more information on creating capped collections.

As an alternative to capped collections, consider MongoDB’s TTL (Time To Live) indexes . As described in Expire Data from Collections by Setting TTL , these indexes allow you to expire and remove data from normal collections based on the value of a date-typed field and a TTL value for the index.

TTL indexes are not compatible with capped collections.

Procedures¶

Create a Capped Collection¶

You must create capped collections explicitly using the db.createCollection() method, which is a helper in the mongo shell for the create command. When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. The size of the capped collection includes a small amount of space for internal overhead.

If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.

Additionally, you may also specify a maximum number of documents for the collection using the max field as in the following document:

The size argument is always required, even when you specify max number of documents. MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

Query a Capped Collection¶

If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.

To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1 , as shown in the following example:

Check if a Collection is Capped¶

Use the isCapped() method to determine if a collection is capped, as follows:

Convert a Collection to Capped¶

You can convert a non-capped collection to a capped collection with the convertToCapped command:

The size parameter specifies the size of the capped collection in bytes.

This holds a database exclusive lock for the duration of the operation. Other operations which lock the same database will be blocked until the operation completes. See What locks are taken by some common client operations? for operations that lock the database.

Tailable Cursor¶

You can use a tailable cursor with capped collections. Similar to the Unix tail -f command, the tailable cursor "tails" the end of a capped collection. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.

Procedures¶

Create a Capped Collection¶

You must create capped collections explicitly using the db.createCollection() method, which is a helper in the mongo shell for the create command. When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection. The size of the capped collection includes a small amount of space for internal overhead.

If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.

Additionally, you may also specify a maximum number of documents for the collection using the max field as in the following document:

The size argument is always required, even when you specify max number of documents. MongoDB will remove older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

Query a Capped Collection¶

If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.

To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1 , as shown in the following example:

Check if a Collection is Capped¶

Use the isCapped() method to determine if a collection is capped, as follows:

Convert a Collection to Capped¶

You can convert a non-capped collection to a capped collection with the convertToCapped command:

The size parameter specifies the size of the capped collection in bytes.

This holds a database exclusive lock for the duration of the operation. Other operations which lock the same database will be blocked until the operation completes. See What locks are taken by some common client operations? for operations that lock the database.

Tailable Cursor¶

You can use a tailable cursor with capped collections. Similar to the Unix tail -f command, the tailable cursor “tails” the end of a capped collection. As new documents are inserted into the capped collection, you can use the tailable cursor to continue retrieving documents.

See Tailable Cursors for information on creating a tailable cursor.

Restrictions and Recommendations¶

Updates¶

If you plan to update documents in a capped collection, create an index so that these update operations do not require a collection scan.

Document Size¶

Changed in version 3.2.

If an update or a replacement operation changes the document size, the operation will fail.

Document Deletion¶

You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.

Sharding¶

You cannot shard a capped collection.

Query Efficiency¶

Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. This is (somewhat) analogous to tail on a log file.

Aggregation $out ¶

The aggregation pipeline stage $out cannot write results to a capped collection.

Transactions¶

Starting in MongoDB 4.2, you cannot write to capped collections in transactions . Reads from capped collections are still supported in transactions .

Capped Collections¶

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

See createCollection() or create for more information on creating capped collections.

TTL indexes are not compatible with capped collections.

Читайте также: