Examples of MongoDB fixed sets (Capped collections)

Source: Internet
Author: User
Tags mongodb

Capped collections is a high performance automatic maintenance object Insertion Order. It is ideal for logging-like functionality

Create a Capped Collection

Unlike standard collection, you have to explicitly create a capped collection,
Specifies the size of a collection, in bytes. The collection data storage space value is allocated in advance.
Note that the specified storage size contains the header information for the database.

> db.createcollection ("Mycoll", {capped:true, size:100000})

Use and Constraints

• In capped collection, you can add new objects.
• Can be updated, however, objects do not increase storage space. If added, the update will fail.
• The database is not allowed to delete. Use the drop () method to delete all collection rows.
Note: After deletion, you must explicitly recreate the collection.
• In 32bit machines, the capped collection is stored for a maximum of 1e9 bytes.
Characteristics
• If space is used, the newly added object replaces the oldest data.
• If you perform find (), no order is specified. The results returned are sorted in the order in which they were inserted.
• Reverse use Find (). Sort ({$natural:-1}).
Application
• Log logging. Capped collection performance is excellent and can be used to store log documents.
• Insert an capped collection speed is very close to storing in the file system.
• Extra Blues Use the built-in LRU mechanism and don't worry about exceeding your hard disk space.
• Cache caching. If you want to cache some small number of objects in the database.
capped collection provides a very convenient mechanism to implement this operation.
• Note that you want to add an index to the capped table because this application reads more frequently than it writes.
• AutoArchive Auto archiving. If you want the data to expire automatically.
capped collection is more convenient than hand-written cron scripts.
Suggestions
• To maximize performance, do not create indexes on capped collection.
• If this collection writes more than read, the index is not needed.
• Note that you may have created the index. The speed will be reduced, but it is still faster than the standard collection.

• Use the natural ordering to more efficiently get the most recently inserted elements. Similar to the Linux Tail command.
Limit the number of objects

You may also need to qualify the number of objects in the collection. If the limit is limited, remove a recent insertion.

Limit the number of objects, specifying the max parameter.

Note: When you specify the number of objects, you must also limit the size of the storage.

To be sure, the storage size is fully capable of storing the restricted number of objects,

Otherwise, the removal of objects is faster. You can use the Validate () tool to view the storage space used by collection,

This allows you to anticipate the space you need.

Note: It is better to limit storage size performance.

Db.createcollection ("Mycoll", {capped:true, size:100000, max:100});
Db.mycoll.validate ();

Tip: When programming, the way to store the version number of the most recent object is to set the Max parameter to 1 (max=1).

Pre-Allocated Ordinary collection
The CreateCollection command can also be used for not capped collections. Such as

Db.createcollection ("Mycoll", {size:10000000});
Db.createcollection ("Mycoll", {size:10000000, autoindexid:false});

You can explicitly create a capped collection by CreateCollection to allow you to specify a number of parameters.

For example, specify the collection size to preload the hard disk space required for collection.

The Autoindexid field can specify whether you want to create a unique index in _id. By default,

Non-capped collection to create an index on the _id.

By default, capped collection does not add an index to the _id.

Sharding
Capped collections does not support data segmentation yet

Check to see if a collection is capped collection
You can call the Iscapped method to see if a collection is capped collection. Db.foo.isCapped ()


Create a fixed collection

We create a fixed collection by CreateCollection, and the capped option is set to true:

>db.createcollection ("Cappedlogcollection", {capped:true,size:10000})

You can also specify the number of documents, plus the max:1000 property:

>db.createcollection ("Cappedlogcollection", {capped:true,size:10000,max:1000})

Determines whether the collection is a fixed collection:

>db.cappedlogcollection.iscapped ()

If you need to convert an existing collection to a fixed collection, you can use the following command:

>db.runcommand ({"converttocapped": "Posts", size:10000})

The above code converts our existing posts collection to a fixed collection.

Fixed collection Query

Fixed collection documents are stored in the order of insertion, by default the query is returned in the order of insertion, or you can adjust the return order by using $natural.

>db.cappedlogcollection.find (). Sort ({$natural:-1})

Functional characteristics of fixed sets

can be inserted and updated, but the update cannot exceed the size of the collection, or the update fails, the deletion is not allowed, but drop () can be called to delete all rows in the collection, but the collection needs to be explicitly rebuilt after the drop.

The maximum value of the last Cappped collection on a 32-bit machine is about the 482.5m,64 bit, which is limited only by the size of the system file.

Fixed collection properties and usage

Property

Property 1: Inserting a fixed set of speed is extremely fast
Property 2: Query output in the order of insertion is extremely fast
Property 3: The ability to eliminate the earliest data when inserting the latest data

Usage

Usage 1: Store log information
Usage 2: Cache a small number of documents

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.