Other ways to MongoDB:
Displays the specified number of data:
1, in MongoDB query a specified number of records, you can use:
Db.collectionname.find (). limit (number);
2, skip the specified number of records in MongoDB, you can use
Db.collectionname.find (). Skip (Number1). Limit (number2); Corresponds to the NUMBER2 data record that shows the beginning of the data in section (NUMBER1+1).
Sort:
Sort the displayed data records in ascending or descending order:
Db.collectionname.find (). Sort ({key:flag}); Key for the field to be sorted based on, Flag 1 is ascending, flag is-1 is descending
When querying records, only the fields of interest are displayed:
Db.collectionname.find ({},{_id:0,key1:1,key2:1})//_id:0 indicates that id,key1:1&key2:1 is not displayed, indicating that the corresponding data for the Key1&key2 is displayed. (0 not shown, 1 shown)
Index:
Index concept: The index is a special data structure, the index is stored in the form of key-value pairs in a data collection that is easy to traverse, key is the value of the field to create the index, and value is the pointer corresponding to the data record block;
Indexes can use the values of one or more columns in a database table, and all values of the field are sorted after the index is created. Indexes are created to improve the efficiency of full-table scans in specific scenarios.
The index in MongoDB:
Use the Ensureindex () method to create an index in MongoDB
DB. Collection_name. Ensureindex({KEY:1})//key value is the index field you want to create, 1 is ascending, 1 is descending
Ensureindex () receives optional parameters, the optional parameter list is as follows:
Parameter |
Type |
Description |
Background |
Boolean |
The indexing process blocks other database operations, and background can specify a later way to create an index, which is to add "background" optional parameters. The default value for "Background" is false. |
Unique |
Boolean |
Whether the established index is unique. Specifies true to create a unique index. The default value is false. |
Name |
String |
The name of the index. If not specified, MongoDB generates an index name by the field name and sort order of the connection index. |
Dropdups |
Boolean |
Whether to delete duplicate records when establishing a unique index, specifying true to create a unique index. The default value is false. |
Sparse |
Boolean |
Do not enable indexing for field data that does not exist in the document; This parameter requires special attention, and if set to true, documents that do not contain corresponding fields are not queried in the index field ... The default value is false. |
Expireafterseconds |
Integer |
Specify a value in seconds, complete the TTL setting, and set the lifetime of the collection. |
V |
Index version |
The version number of the index. The default index version depends on the version that is run when the index is mongod created. |
Weights |
Document |
The index weight value, which is between 1 and 99,999, that represents the score weight of the index relative to the other indexed fields. |
Default_language |
String |
For a text index, this parameter determines the list of rules for the stop word and the stemming and the word changer. Default to English |
Language_override |
String |
For a text index, this parameter specifies the name of the field contained in the document, the language overrides the default language, and the default value is language. |
MongoDB Learning Notes (ii)