3.44. MongoDB fixed set (Capped Collections)

发布时间 :2025-10-25 12:32:57 UTC      

MongoDB fixed set (Capped Collections) is a collection with excellent performance and fixed size. For a fixed size, we can imagine that it is like a circular queue. When the set space is used up, the inserted element will cover the original header element!

3.44.1. Create a fixed collection

We create a fixed collection through 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 attribute:

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

Determine whether the set is a fixed set:

>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 into a fixed collection.

3.44.2. Fixed set query

Fixed collection documents are stored in insertion order, and queries are returned in insertion order by default, or you can use $natural to adjust the return order.

>db.cappedLogCollection.find().sort({$natural:-1})

3.44.3. The functional characteristics of fixed sets

You can insert and update, but the update cannot exceed the size of the collection, otherwise the update fails and deletion is not allowed, but you can call drop () to delete all rows in the collection, but you need to explicitly rebuild the collection after drop.

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

3.44.4. Fixed set attributes and usage

Attribute

  • Attribute 1: inserting a fixed collection is extremely fast

  • Attribute 2: query output in insertion order is extremely fast

  • Attribute 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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.