Mongodb Guide (translated) (18)-developer zone-index (2) _ id index, composite index, sparse index, array index, unique index

Source: Internet
Author: User

_ Id Index

An index is automatically created for the _ id field for all sets except the set of capacity. This is a special index and cannot be deleted. The _ id index forces that its keywords are unique (except for some scenarios in the partition environment ).

The _ id value remains unchanged.

Index embedded keywords ("dot notation ")

In Mongodb, You Can index Keywords of embedded documents. The method used to access the sub-document is treated as dot notation. For example:

db.things.ensureIndex({"address.city": 1})

Document as keyword

The indexed fields can be of any type, including (embedded) documents:

db.factories.insert( { name: "xyz", metro: { city: "New York", state: "NY" } } );
db.factories.ensureIndex( { metro : 1 } );
// this query can use the above index:
db.factories.find( { metro: { city: "New York", state: "NY" } } );
// this one too, as {city:"New York"} < {city:"New York",state:"NY"}
db.factories.find( { metro: { $gte : { city: "New York" } } } );
// this query does not match the document because the order of fields is significant
db.factories.find( { metro: { state: "NY" , city: "New York" } } );

The alternative to using documents as keywords is to create a composite index:

db.factories.ensureIndex( { "metro.city" : 1, "metro.state" : 1 } );
// these queries can use the above index:
db.factories.find( { "metro.city" : "New York", "metro.state" : "NY" } );
db.factories.find( { "metro.city" : "New York" } );
db.factories.find().sort( { "metro.city" : 1, "metro.state" : 1 } );
db.factories.find().sort( { "metro.city" : 1 } )

Both methods have their own advantages and disadvantages. When the entire (sub) document is used as a keyword, the comparison order is predefined and sorted by the ascending order of the keyword in the BSON document. In a composite index, you can mix the ascending and descending keywords. The query optimizer can still query only the first keyword in the index.

Composite Index

In addition to a single keyword index, Mongodb also supports multiple "combined" indexes. As with the basic index, you can use ensureIndex () in shell to create a composite index. However, unlike the basic index, you can specify only one keyword. In this case, you can specify multiple:

db.things.ensureIndex({j:1, name:-1});

When an index is created, the number following the keyword specifies the direction of index sorting, so it can only be 1 (ascending) or-1 (descending ). The direction is not important for single-key indexes or random access, but when you sort the composite indexes or query the range, the direction is very important.

If you have a composite index built on multiple fields, you can use it to query the subset fields before the multi-key. Therefore, if you have an index in:

A, B, c

You can use it to query these fields:

A

A, B

A, B, c

Index elements in a logarithm Group

If the field to be indexed in the document is an array, Mongodb will create an index for each element in the array. Go to the create multiple Chapters Page to view details.

 

Sparse Index

"Sparse Index" is the index of a document that contains only indexed fields.

Any document without a sparse index field will not be stored in this index. This index is called a sparse index because it does not contain any document without an index field.

According to the definition, sparse indexes are incomplete (for this set) and different from full indexes. When "sparse Index" is used for sorting (or filtering), some documents in the set may not be returned. This is because only the documents in this set will be returned.

> db.people.ensureIndex({title : 1}, {sparse : true})
> db.people.save({name:"Jim"})
> db.people.save({name:"Sarah", title:"Princess"})
> db.people.find()
{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
> db.people.find().sort({title:1}) // only 1 doc returned because sparse
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }
> db.people.dropIndex({title : 1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.people.find().sort({title:1}) // no more index, returns all documents
{ "_id" : ObjectId("4de6abd5da558a49fc5eef29"), "name" : "Jim" }
{ "_id" : ObjectId("4de6abdbda558a49fc5eef2a"), "name" : "Sarah", "title" : "Princess" }

You can combine sparsity and uniqueness to create unique constraints to ignore documents without corresponding fields.

Note that the sparse index of Mongodb is not a blocking index. Mongodb sparse indexes can be considered as dense indexes with specified filters.
Unique Index

Mongodb supports unique indexes. It ensures that documents that have duplicate index keywords with existing documents are not inserted. There are no duplicate values in the fields firstname and lastname. You can do this:

db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

Unique index and nonexistent keyword

If the document saved to the set has no value in the index field, its index field is assigned null and then inserted. That is to say, you cannot insert multiple documents with no value in a single index field.

db.things.ensureIndex({firstname: 1}, {unique: true});
db.things.save({lastname: "Smith"});
// Next operation will fail because of the unique index on firstname.
db.things.save({lastname: "Jones"});

DropDups

If a field already has a duplicate value, a unique index cannot be created for the field. If you need to create a unique index in any case, and only keep the first document in the database that appears in the field, and then delete all documents with duplicate values in the field, add the "dropDups" option:

db.things.ensureIndex({firstname : 1}, {unique : true, dropDups : true})
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.