Index type
Although the index of MongoDB is the same on the storage structure, it is divided into several types such as unique index (unique), sparse index (sparse), multivalued index (multikey) according to different application layer requirements.
Unique index
The unique index is created with the option to add unique:true, and the command is created as follows:
Db.users.ensureIndex ({username:1}, {unique:true})
When the above unique index is created, if you insert a username data that already exists, the following error is reported:
E11000 duplicate key error index:gardening.users. $username _1 dup key: {: "Kbanker"}
If you create a unique index on the collection of an existing data, if the corresponding field of the unique index has duplicate data items, then the creation will fail, we need to add a dropdups option to force the deletion of duplicates, the following example:
Db.users.ensureIndex ({username:1}, {unique:true, dropdups:true})
Loosely indexed
If you have some rows in your data that do not have a field or field value NULL, then if you establish a normal index on this field, then the row without this field or value Null will also participate in the index structure, occupying the appropriate space. If we do not want those rows with empty values to participate in our index, this can be loosely indexed, and a loose index will only allow rows that are not empty for the specified field to participate in the index creation. To create a loose index, you can use the following command:
Db.reviews.ensureIndex ({user_id:1}, {sparse:true})
Multi-valued Index
MongoDB can create an index on an array type, like the following structure, where MongoDB can create an index on the tags field:
{Name: "Wheelbarrow", Tags: ["Tools", "gardening", "Soil"]}
When the index is built, three index elements are generated for each of the three values in the tags, and the Tools,gardening,soil three values in the index point to the same row of data. The equivalent of splitting into three separate index entries.
Index management index creation and deletion
There are many ways to create and delete an index, and the following two are the original methods, and the creation of the index is done by writing to the System.indexes collection:
Spec = {ns: "Green.users", key: {' Addresses.zip ': 1}, Name: ' Zip '}db.system.indexes.insert (spec, true)
The above command writes a record to system.indexes to create the index, which contains the namespace of the collection to create the index above, the index information, and the name of the index.
Once created, we can find the index we created with the following command:
Db.system.indexes.find () {"_id": ObjectId ("4d2205c4051f853d46447e95"), "ns": "Green.users", "key": {"Addresses.zip": 1}, "name": "Zip", "V": 0}
To delete a created index, we can use the following command to implement:
Use Greendb.runcommand ({deleteindexes: "Users", Index: "Zip"})
CREATE INDEX command
In fact, there are more convenient commands for creating indexes, that is, ensure index, for example, when we create a federated index of open and close two fields, you can use the following command:
Db.values.ensureIndex ({open:1, close:1})
This command triggers the two process of index creation, one is to sort the corresponding field, because the index is organized by the B + tree, to build the tree, to sort the data to improve the efficiency of inserting the B + tree (the efficiency of the second process), in the log, you can see the output similar to the following:
Tue Jan 4 09:58:17 [CONN1] Building new index on {open:1.0, close:1.0} for stocks.values1000000/4308303 23%2000000/430 8303 46%3000000/4308303 69%4000000/4308303 92%tue Jan 4 09:59:13 [conn1] external sort used:5 files in secs
The second procedure is to insert the sorted data into the index structure to make available indexes:
1200300/4308303 27%2227900/4308303 51%2837100/4308303 65%3278100/4308303 76%3783300/4308303 87%4075500/4308303 94% Tue Jan 4 10:00:16 [conn1] done building bottom layer, going to committue Jan 4 10:00:16 [conn1] did for 4308303 Records 118.942secsTue Jan 4 10:00:16 [conn1] Insert stocks.system.indexes 118942ms
In addition to the output in the log, you can also get information about the current operating thread by executing the currentop command at the terminal, as in the following example:
> Db.currentop () {"InProg": [{"opid": +, "active": true, "LockType": "Write", "Waitingforlock": false, "secs_running" : "OP": "Insert", "ns": "Stocks.system.indexes", "query": {}, "client": "127.0.0.1:53421", "desc": "conn", "MSG": "In Dex: (1/3) External sort 3999999/4308303 92% "}]}
The final part is an index build process that is currently performing the sort process to 92%.
Create an index in the background
Creating an index adds a write lock to the database and, if the dataset is large, suspends the operation of the read-write database on the line to wait for the index creation to end. This affects the normal service of the database, we can make the creation work in the background by the option of adding background:true when the index is created, when the index is created or a write lock is required, but the write lock is not directly exclusive to the index creation, but pauses to make way for other read and write operations. Not cause serious performance impact. Specific methods:
Db.values.ensureIndex ({open:1, close:1}, {background:true})
Create an index offline
In any case, the creation of indexes will put some pressure on the database, thus affecting the online service. If the process of creating an index does not affect the online service at all, we can add it to the replica sets by stripping the nodes in the replica sets from the cluster first, adding the appropriate indexes on the node. This only need to guarantee a condition, that is, the time to create the index can not be longer than Oplog can save the time of the log, or after the creation of the node again on-line discovery can no longer catch up with primary, this will be Resync operation.
Index Backup
We know that whether you use the Mongodump or the Mongoexport command, you simply back up the data and you cannot back up the index. When we recover, we still need to wait for a lengthy index creation process. So if you want to back up with an index, it's a good idea to use a backup data file.
Index compression
Index after the use of a period of time, experience additions and deletions and other operations, will become relatively loose, so that the war with unnecessary space, we can through the Reindex command, re-organize the index, so that the space footprint of the index becomes smaller.
MongoDB Index related article-excerpt from the network