MONGODB CREATE Index
> Db.foo.ensureIndex ({"X": 1, "Y":-1})
Use Ensureindex to create indexes, 1 for ascending, 1 for reverse
Tip: Query to return more than half of the results in the collection, but there is no need to create an index, directly scanned data will be more efficient
> Db.test.ensureIndex ({"X": 1}, {"Unique": true})
The above statement is to create a unique index , there may be duplicate data before creating a unique index, we can use "dropdups" to recklessly delete these duplicate data, write the following
> Db.test.ensureIndex ({"X": 1}, {"Unique": true, "dropdups": true})
Of course, it's better to write a script to preprocess it. This data is a better solution for creating a unique index
When creating an index, we can create a name for the cable instead of a name that is automatically generated by the system, using the following notation:
> Db.test.ensureIndex ({"X": 1}, {"Name": "Myindex"})
When we have a large collection of data, creating an index is a time-consuming and laborious task, and we can write the statement that creates the index, which is done in the background by the process of creating the index:
> Db.test.ensureIndex ({"X": 1}, {"Background": true})
When the index is no longer used we can use dropindexes to delete the index, as follows:
> Db.runcommand ({"dropindexes": "Test", "index": "Myindex"})
The parameter followed by the Dropindexes is the collection name, and the parameter followed by index is the name of the indexed, and if you need to delete all indexes, use "*" to
MONGODB CREATE Index