MONGODB Note 04

Source: Internet
Author: User

Special indexes and collections

1. Fixed set: The fixed set needs to be created in advance to look good, and its size is fixed. When a fixed collection is full, if you insert a new document again, the pinned collection automatically removes the oldest document from the collection.

2. Create a fixed collection: Db.createcollection ("My_collection", {"capped": true, "size": 10000}) Create a fixed set of 10000 bytes

In addition to the size, createcollection can also specify the number of documents in the fixed set: Db.createcollection ("My_collection", {"capped": true, "size": 10000, "Max": 10})

When specifying the number of documents, you must specify both the collection size. The number of documents in a fixed collection cannot exceed the limit on the number of documents, and the size of the fixed collection cannot exceed the size limit.

3. Convert a regular collection to a fixed collection: Db.runcommand ({"converttocapped": "Test", "Size": 10000}) cannot convert a fixed collection to a non-fixed collection.

4. Natural sort: Natural sort returns the order of the documents in the result set is the order of the documents on disk. Only the natural sort in a fixed set makes sense, and the location of the document changes frequently for other collections, with little meaning in natural ordering.

5. Loop cursor: A special kind of cursor that will not be closed when the result set of the loop cursor is taken out of the light. When a new document is inserted into the collection, the loop cursor continues to fetch the result. Because normal collections do not maintain the order in which documents are inserted, circular cursors can only be used on fixed sets.

6. Call CreateCollection the Specify AUTOINDEXID option when creating the collection is false, and creating the collection does not automatically create an index on "_id". This is not recommended in practice, but it does bring a slight increase in speed for a collection that has only an insert operation.

7. TTL index: If you need a more flexible aging removal system, you can use the TTL index, which allows you to set a time-out for each document. A document is deleted after it reaches the pre-set aging level. This type of index is useful for caching problems.

Db.foo.ensureIndex ({"lastupdated": 1},{"Expireaftersecs": 60*60*24})//Expiration Time 24 hours

MongoDB does a clean-up of the TTL index every minute, so you should not rely on the time in seconds to guarantee the index's survival status. You can use the Collmod command to modify the value of Expireaftersecs:

Db.runcommand ({"Collmod": "Someapp.cache", "Expireaftersecs": 3600})

There can be more than one TTL index on a given collection, but the TTL cannot be a composite index.

8. Full-text index: not recommended, performance impact is too large.

9. Geospatial Index: Most commonly used are 2dsphere indexes (maps for the Earth's surface type) and 2d indexes (for planar maps and time-continuous data)

10. Using Gridfs to store files: Gridfs is a storage mechanism for MongoDB that stores large binary files.

Advantages:

A. Simplify your stack and use Gridfs instead of a standalone file storage tool.

B. Fault-escaping or scale-out of file storage is easier

C. Some of the other file system problems that may be encountered can be resolved in a more leisurely manner.

D. A higher concentration of file storage

Disadvantages:

A. Low performance: Accessing files from MongoDB is faster than accessing files directly from the file system.

B. If you want to modify the document on the Gridfs, you can only delete the existing document first.

In general, if you have some large files that don't change frequently but often require continuous access, then using Gridfs is a good fit. (What about performance?) Because it's cached to memory? )

The idea behind Gridfs: You can split large files into larger chunks, storing each as a separate document. There is a document that organizes these blocks together and stores the meta information for that document.

12. You can use any custom field to save the required file meta information. You may want to save the file's download count, MIME type, or user rating in the file meta information.

MONGODB Note 04

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.