Mongodb study notes (b): Index

  Mongodb is based on a set of index (Index), an index similar to the role of traditional relational database, the purpose is to improve query speed. If there is no index, Mongodb must scan all the documents record set when reading data. This full set of scanning efficiency is very low, especially when dealing with large data, the query may take tens of seconds to several minutes, which is based on Internet applications, web sites can not be tolerated. When the collection indexing, query scans the index content, and not to scan the corresponding collection. But at the same index, is the need to add additional storage overhead; in the case has been indexed, if the newly inserted collection of documentation, it will cause the index reordering, this process will affect the query speed. Mongodb index B-tree data structure is formed and the corresponding algorithm. By default, while building the collection, MongoDB database collections _id automatically create a unique index, you can avoid repeating the same record into the document _id values.

  Create a single field index:  

     db.student.createIndex ({age: 1}) age field name 1 -1 ascending descending.

  Create a unique index field values:

      ({: "text" name}, {db.student.createIndex UNIQUE: to true }) single unique index field values, text is text index

    Multi-unique index field values ​​db.student.createIndex ({name: "text", age:: 1}, {true unique}) 

  Create a hash index:

    db.student.createIndex({_id:"hashed"})

  Query index on the set:

     db.student.getIndexes()

  Delete all of the index set:  

    db.student.dropIndexes()

  Delete the specified index of the collection:

    db.student.dropIndex(index)

  Rebuild all indexes collection:

    db.student.reIndex()

  Query index size of the collection:

    db.student.totalIndexSize()

Guess you like

Origin www.cnblogs.com/jasonbourne3/p/11058596.html