Detailed explanation of MongoDB fixed collection
Generally, the collection we create has no size. You can keep adding documents to it. This collection can grow dynamically. There is also a collection in MongoDB called a fixed collection. The size of this collection is fixed. I can Set the number of documents in the collection when creating it, assuming it is 100. When the number of documents in the collection reaches 100, if you insert another document into the collection, only the latest 100 documents will be retained, and the previous documents will be will be deleted. Generally, we can use fixed collections for log information, and other data that need to be deleted regularly can also use fixed collections. In this article, we will take a look at the use of this fixed collection.
This article will share with you a detailed explanation of MongoDB fixed collections.
1.Installing MongoDB on Linux
2.Basic MongoDB operations
3.MongoDB data types
4.MongoDB document update operations
5.MongoDB documents Query operation (1)
6.MongoDB document query operation (2)
7.MongoDB document query operation (3)
8.MongoDB view execution plan
9. First introduction to the index in MongoDB
10. Various types of indexes in MongoDB
Creation
The creation method of fixed collection is also relatively simple, as follows:
db.createCollection("my_collect",{capped:true,size:10000,max:100})
capped:true parameter representation This collection is a fixed-size collection, size represents the size of the collection in kb, and max represents the maximum number of documents in the collection. What we are doing here is equivalent to giving two constraints to the fixed collection. As long as any one of the constraints is met, the collection will start to delete older data. Once a fixed collection is successfully created, it cannot be modified. If you want to modify it, you can only delete it and start again. At this point we can try to add 120 simple pieces of data to the collection, and then we will find that the earliest 20 pieces of data disappear.
In addition to directly creating a fixed collection, we can also convert an ordinary collection into a fixed collection through the convertToCapped operation, as follows:
db.runCommand({convertToCapped:"sang_collect",size:10})
Natural sorting problem
Natural sorting It is to arrange the documents according to the order in which they are on the disk. Natural sorting in ordinary collections does not make much sense, because the position of the documents is always changing, while the documents in the fixed collection are saved in the order in which they are inserted. Natural order is the insertion order of documents, so we can use natural ordering to sort documents from old to new, as follows:
db.sang_collect.find().sort({$natural:1})
You can also sort from new to old:
db.sang_collect.find().sort({$natural:-1})
in a fixed collection Other operations are basically the same as ordinary collections and will not be described here.
The above content is a detailed explanation of MongoDB fixed collections. I hope it can help everyone.
MongoDB tips and precautions summary
Laravel tutorial on how to use mongodb database
How to improve MongoDB Security
The above is the detailed content of Detailed explanation of MongoDB fixed collection. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.
