Creating special indexes

In addition to all the indexes types we've created until now, whether in ascending or descending order, or text typed, we have three more special indexes: time to live, unique, and sparse.

Time to live indexes

The time to live (TTL) index is an index based on lifetime. This index is created only in fields that are from the Date type. They cannot be compound and they will be automatically removed from the document after a given period of time.

This type of index can be created from a date vector. The document will expire at the moment when the lower array value is reached. MongoDB is responsible for controlling the documents' expiration through a background task at intervals of 60 seconds. For an example, let's use the customers collection we have been using in this chapter:

{ 
"_id" : ObjectId("5498da405d0ffdd8a07a87ba"), 
"username" : "customer1", 
"email" : "[email protected]", 
"password" : "b1c5098d0c6074db325b0b9dddb068e1", "accountConfirmationExpireAt" : ISODate("2015-01-11T20:27:02.138Z") 
}

The creation command that is based on the time to live index for the accountConfirmationExpireAt field will be the following:

db.customers.createIndex(
{accountConfirmationExpireAt: 1}, {expireAfterSeconds: 3600}
)

This command indicates that every document that is older than the value in seconds requested in the expireAfterSeconds field will be deleted.

There is also another way to create indexes based on lifetime, which is the scheduled way. The following example shows us this method of implementation:

db.customers.createIndex({
accountConfirmationExpireAt: 1}, {expireAfterSeconds: 0}
)

This will make sure that the document you saw in the previous example expires on January 11 2015, 20:27:02.

This type of index is very useful for applications that use machine-generated events, logs and session information, which need to be persistent only during a given period of time, as you will see once again in Chapter 8, Logging and Real-time Analytics with MongoDB.

Unique indexes

As with the vast majority of relational databases, MongoDB has a unique index. The unique index is responsible for rejecting duplicated values in the indexed field. The unique index can be created from a single or from a multikey field and as a compound index. When creating a unique compound index, there must be uniqueness in the values' combinations.

The default value for a unique field will always be null if we don't set any value during the insert operation. As you have seen before, the index created for the _id field of a collection is unique. Considering the last example of the customers collection, it's possible to create a unique index by executing the following:

db.customers.createIndex({username: 1}, {unique: true})

This command will create an index of the username field that will not allow duplicated values.

Sparse indexes

Sparse indexes are indexes that will be created only when the document has a value for the field that will be indexed. We can create sparse indexes using only one field from the document or using more fields. This last use is called a compound index. When we create compound indexes, it is mandatory that at least one of the fields has a not-null-value.

Take as an example the following documents in the customers collection:

{ "_id" : ObjectId("54b2e184bc471cf3f4c0a314"), "username" : "customer1", "email" : "[email protected]", "password" : "b1c5098d0c6074db325b0b9dddb068e1" }
{ "_id" : ObjectId("54b2e618bc471cf3f4c0a316"), "username" : "customer2", "email" : "[email protected]", "password" : "9f6a4a5540b8ebdd3bec8a8d23efe6bb" }
{ "_id" : ObjectId("54b2e629bc471cf3f4c0a317"), "username" : "customer3", "email" : "[email protected]" }

Using the following example command, we can create a sparse index in the customers collection:

db.customers.createIndex({password: 1}, {sparse: true})

The following example query uses the created index:

db.customers.find({password: "9f6a4a5540b8ebdd3bec8a8d23efe6bb"})

On the other hand, the following example query, which requests the descending order by the indexed field, will not use the index:

db.customers.find().sort({password: -1})
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.151