Transferred from: http://blog.csdn.net/mniwc/article/details/8465584
indexes can often greatly improve the efficiency of queries. When you use queries in your system, you should consider establishing related indexes. Creating an index in MongoDB is relatively easy.
The index in MongoDB is conceptually the same as most relational databases such as MySQL. When you need to build an index in MySQL in some cases, this scenario is also appropriate for MongoDB.
Basic Operations
An index is a data structure that collects a set of values for a particular field in a document. MongoDB's query optimizer can use this data structure to quickly find and sort documents (collection) in Collections (collection). To be precise, these indexes are implemented by B-tree indexes.
On the command line, you can create an index by calling the Ensureindex () function, which specifies one to multiple fields that require an index. Following the example in the previous essay, we then set up the index on the J field in the Things collection:
> Db.things.ensureIndex ({j:1})
The Ensureindex () function is created only if the index does not exist.
Once the collection is indexed on a field, the random query for that field is accessed quickly. If there is no index, MongoDB iterates through all the key-value pairs, and then goes to the corresponding check-related fields.
> Db.things.find ({j:2}); Query on indexed fields, fast {"_id": ObjectId ("4e24433dcac1e3490b9033be"), "X": 4, "J": 2} > Db.things.find ({x:3}); /query on fields that are not indexed, need to match fields one by one, slow {"_id": ObjectId ("4E244315CAC1E3490B9033BC"), "X": 3}
You can view all the indexes in the current collection by entering Getindexs () on the command line.
> db.things.getIndexes () [{"Name": "_id_", "ns": "Things.things", "key": {"_id": 1} , "V": 0}, {"_id": ObjectId ("4e244382cac1e3490b9033d0" ns ":" Things.things "," key ": {" J ": 1}," name ":" J_1 "," V ": 0}]
Ability to return all indexes in the current database via Db.system.indexes.find ()
> Db.system.indexes.find () {"Name": "_id_", "ns": "Things.things", "key": {"_id": 1}, "V": 0} {"_id" : ObjectId ("4e244382cac1e3490b9033d0"), "ns": "Things.things", "key": {"J": 1}, "name": "J_1", "V": 0}
Default Index
For each collection (except for the capped collection), the default is to create an index on the _id field, and this particular index cannot be deleted. The _id field is mandatory and is maintained by the database.
Nested keywords
In MongoDB, you can even build an index on an embedded document (embedded).
> Db.things.ensureIndex ({"Address.city": 1})
Document as Index
Any type, including document, can be indexed:
> Db.factories.insert ({name: "xyz", Metro:{city: "New York", State: "NY"}); > Db.factories.ensureIndex ({metro:1}); > Db.factories.find ({metro:{city: "New York", State: "NY"});//ability to query with index {"_id": ObjectId (" 4e244744cac1e3490b9033d2 ")," name ":" XYZ "," Metro ": < {" City ":" New York "," state ":" NY "}} > Db.factor Ies.find ({metro:{$gte: {City: "New&n