Fixed collection:
Property 1. Insert the fixed set very quickly
Attribute 2: Query output in the order of insertion is extremely fast
Attribute 3: Ability to retire the oldest data when inserting the latest data
Usage 1: Store log information
Usage 2: Cache a few documents
Create a pinned Collection
Unlike normal collections, fixed collections need to be explicitly created using the createcollection command to create
Db.createcollection ("My_collection", {capped:true, size:10000})
Creates a fixed collection of ' my_collection ', with a size of 10000 bytes, and can also limit the number of documents, plus the Max:100 property
Note: Specify the upper limit of the document, you must specify the size. Document limits are eliminated when capacity is not full, and if full, it is eliminated based on capacity limits.
Convert Collection
To convert a normal set to a fixed need to use the converttocapped command
Db.rencommand ({converttocapped: "Test", size:10000})
Converts a common test set to a fixed set, with a size of 10000 bytes
Natural sort
Fixed collection documents are stored in the order in which they are inserted, by default the query is returned in the order of insertion, or the return order can be adjusted using $natural
Db.my_collection.find (). Sort ({"$natural": 1})
Judge with db.test.isCapped ();
Display Status: Db.test.status ();
Gridfs
Gridfs is a mechanism for storing binary files in MongoDB, using Gridfs for the following reasons:
1. Store huge files, such as videos, high-definition images, etc.
2. Use Gridfs to simplify requirements
3.GridFS will directly take advantage of the established replication or fragmentation mechanisms, and recovery and expansion are easy
4.GridFS can prevent users from uploading content file system problems
5.GridFS does not produce disk fragmentation
Gridfs using two tables to store data
file contains Metadata objects
Chunks binary blocks that contain some other relevant information
In order for multiple Gridfs to be named a single database, the file box block has a prefix, by default, the prefix is FS, so any default Gridfs storage will include namespace Fs.files and Fs.chunks.
Various third-party languages can change their prefixes
Using the Gridfs mongofiles command
Mongofiles is a tool for manipulating Gridfs from the command line
Three commands: put (store) get (GET) List (list)
Index
MongoDB provides a variety of index support, index information is saved in System.indexes, MongoDB _id field at the time of creation, the default has been indexed, the index is very special, and can not be deleted, but capped collections exception
Build an index
1. Create a normal index
To establish a normal index function: Ensureindex ()
Index on name, 1 for ascending, 1 for descending, default to 1
Db.persons.ensureIndex ({name:1});
When the system already has a large amount of data, creating an index is time consuming and needs to be performed in the background by simply specifying "Backgroud:true"
Db.persons.ensureIndex ({name:1},{background:true});
2. Create a unique index
Simply specify "Unique:true" in the Ensureindex command to create a unique index
Db.test.ensureIndex ({name:1}, {unique:true});
3. View Index Command:
Db.test.getIndexes ();
Db.test.getIndexKeys ();
4. Deleting an index
Delete all indexes in test table
Db.test.fropIndexes ();
Delete the name index from the test table
Db.test.dropIndex ({name:1});
To parse an SQL statement using the index condition command:
Db.test.find (). explain ();
Performance optimization:
Explain execution plan
MongoDB provides a explain command to learn how a query request is handled by the system. The explain command provides a good view of how the system uses indexes to speed up retrieval while targeting optimized indexing
Explain return parameter meaning:
Curser: Returns the cursor type (basiccursor or btreecursor)
nscanned: Number of documents scanned
Nscannedobjects
N: The number of questions returned
Millis: Time-consuming (milliseconds)
Indexbounds: The index used
Optimizer profile
MongoDB Database Porfiler is a slow query log feature
Turn on the profiling function
There are two ways to control the switch and level of the profiling
When you start MongoDB, add the--profile= level
1.db.setprofilinglevel (2);
The level of profile can take 0,1,2:
0 means no open
1 indicates slow log command (default query time is 100ms)
2 means record all commands
Set slow query time
1.db.setprofilinglevel (1, 10);//The second parameter is a slow query time
2. Configure by adding--SLOWMS boot parameters
Performance monitoring:
Mongosniff
This tool can monitor from the bottom to what commands are sent to MongoDB to perform
./mongosniff--source NET Lo
This tool is dynamically monitored in real time and needs to be opened by another client for command operation
This tool will output these operational data records to a log file