In this blog post, we mainly introduce database indexes. Main include create index, DROP index, list index
The database driver provides a way to create, delete, and view indexes on the collection.
Create an index
In an operation that creates an index, you can create one index at a time, or you can create multiple indexes at once. In the 3.0 and later versions of the MONGODB database, multiple indexes are created in parallel. In earlier versions, multiple indexes were created in order.
Creation of a single index, using the Create_one method
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') Client[:actors].indexes.create_one ({: name= >1},unique:true)
Multiple indexes are created, using the Create_many method. It is important to note that when multiple indexes are created at one time, the key values of the index must be passed by the corresponding specific key-value pair as described. This is because the parameters of each created index may be different.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') Client[:actors].indexes.create_many ([{: key= >{name:1},unique:true}, {: Key=>{label:-1}}])
The complete list of parameters available when creating an index is listed in the following table
Parameters |
Parameter description |
: Background |
Evaluates to TRUE or FALSE, explicitly when the index is created in the background or foreground execution |
: Expire_after |
The number of seconds to expire the document from the current time |
: Name |
The name of the index |
: Sparse |
Determines whether the index is sparse, either true or False |
: Storage_engine |
Name of the storage engine that defines the index |
: Version |
version of the indexed format used |
:d Efault_language |
Default language for text indexing |
: Language_override |
The name of the domain to use when overriding the default language |
: text_version |
Format version for storing text indexes |
: Weights |
Documents that specify fields and weights in a text search |
: sphere_version |
Version of the 2-D index |
: Bits |
Set the maximum boundary of latitude and longitude in a 2-bit index |
: Max |
Maximum boundary value for latitude and longitude of a 2-D index |
: Min |
Minimum boundary value for latitude and longitude of 2-D index |
: bucket_size |
Grouping Geo Haystack index position values is the number of units adapted |
:p artial_filter_expression |
Parallel Index Expression Filters |
2. Deleting an index
Delete index, use Dropone or Dropall
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') Client[:bands].index.drop_one (' Name_1 ') Client[:bands],drop_all
3. Listing indexes
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') Client[:bands].indexes.each do |index| P Index End
This article from "Techfuture" blog, declined reprint!
Ruby Operation MongoDB (Advanced six)-index indexing