MONGODB Special Indexes and collections

Source: Internet
Author: User

Fixed collection (for logging)

A fixed set needs to be created in advance, 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.

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. The limit has been first reached.

Create a pinned Collection

Db.createcollection ("My_collection", {"capped": true, "size": 100000, ' Max ': 100});

Capped: Fixed set

My_collection: Set noun

Size: Collection Sizes

Max: Number of documents

Convert a normal collection to a fixed collection

Db.runcommand ({"converttocapped": "Test", "Size": 10000});

Cannot convert a fixed collection to a normal collection

Natural sort

A special sort can be performed on a fixed set, called a natural sort (naturalsort). Natural sort returns the order of the documents in the result set is the order of the documents on disk.

Documents that are naturally sorted are arranged from old to new. Of course, they can be sorted from new to old.

Db.my_collection.find (). Sort ({"$ natural":-1})

Circular cursors

A circular cursor (tailablecursor) is a special kind of cursor that is not closed when the result set of a loop cursor is taken out of the light. The loop cursor is inspired by the tail-f command (a circular cursor is a bit similar to this command) and will continue to extract the output as long as possible. Because the loop cursor is not closed after the result set has been taken, the loop cursor continues to fetch the result when a new document is inserted into the collection. Because normal collections do not maintain the order in which documents are inserted, circular cursors can only be used on fixed sets.

TTL index-index with life cycle

Create TTL index

Db.foo.ensureIndex ({"LastUpdated": 1}, {"Expireaftersecs": 60* 60* 24})

This establishes a TTL index on the "LastUpdated" field. If the "lastupdated" field of a document exists and its value is a date type, the document is deleted when the server time is expireaftersecs seconds later than the LastUpdated field in the document.

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})

The TTL index cannot be a composite index, but it can be used as a "normal" index to optimize sorting and querying.

Geo-Spatial Index

2dsphere

Allows you to specify points, lines, and polygons using the Geojson format.

A point can be expressed as an array of two elements in the form of [Longitude,latitude] ([longitude, Latitude]):

{' name ': ' New York city ', ' loc ': {' type ': ' point ', ' coordinates ': [50, 2]}}

A line can be represented by an array of dots:

{' name ': ' Hudson River ', ' loc ': {' type ': ' line ', ' coordinates ': [[0,1], [0,2], [+]}}

Polygons are represented as lines (all arrays of dots), but "type" is different:

{"Name": "New England", "loc": {"type": "Polygon", "coordinates": [[0,1], [0,2], [[]]}}

Create an index

Db.world.ensureIndex ({"loc": "2dsphere"})

Intersection Query $geoIntersects

var eastvillage = {... "Type": "Polygon", ... "Coordinates": [... [-73.9917900, 40.7264100], ... [-73.9917900, 40.7321400], ... [-73.9829300, 40.7321400], ... [-73.9829300, 40.7264100] ...]}

Db.map.find ({"loc": {"$geoIntersects": {"$geometry": Eastvillage}}})

Contains query $within

Db.map.find ({"loc": {"$within": {"$geometry": Eastvillage}}})

Find nearby $near

Db.map.find ({"loc": {"$near": {"$geometry": Eastvillage}}})

Index

For aspheric maps (game maps, time-continuous data, and so on). Only points can be indexed.

Data type [22,22] coordinates

Create an index

Db.world.ensureIndex ({"Tile": "2d"})

Contains query $near

Db.world.find ({"Tile": {"$ near": [20, 21]}})

Nearby query $within $box represent rectangles

Db.hyrule.find ({"Tile": {"$ within": {"$ box": [[10, 20], [15, 30]]}})

MONGODB Special Indexes and collections

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.