You are on page 1of 17

U18CSI5203-NOSQL

DATABASES

Mr. G. KANAGARAJ & Ms.C.BHARATHIPRIYA


Click to add text

Department of Computer Science & Engineering

WEEK 4
Indexing Properties in MongoDB
MongoDB Index Properties

• Unique Indexes
• Partial Indexes
• Sparse Indexes
• TTL Indexes
• Hidden Indexes
Unique Indexes
• The unique property for an index causes MongoDB
to reject duplicate values for the indexed field.

• Other than the unique constraint, unique indexes


are functionally interchangeable with other
MongoDB indexes.
Create a Unique Index
• To create a unique index, use the
> db.collection.createIndex() method with the unique option
set to true.

> db.collection.createIndex( <key and index type


specification>, { unique: true } )
• Unique Index on a Single Field
For example, to create a unique index on the user_id field of
the members collection, use the following operation in the
mongo shell:

>db.members.createIndex( { index": 1 }, { unique: true } )


Partial Indexes
• Partial indexes only index the documents in a
collection that meet a specified filter expression.
• By indexing a subset of the documents in a collection,
partial indexes have lower storage requirements and
reduced performance costs for index creation and
maintenance.
• Partial indexes offer a superset of the functionality of
sparse indexes and should be preferred over sparse
indexes.
Create a Partial Index
• To create a partial index, use the,
➢db.collection.createIndex() method with the
partialFilterExpression option.

The partialFilterExpression option accepts a document


that specifies the filter condition using:

• equality expressions (i.e. field: value or using the $eq operator),


• $exists: true expression,
• $gt, $gte, $lt, $lte expressions,
• $type expressions,
• $and operator at the top-level only
For example, the following operation creates a
compound index that indexes only the documents with a
rating field greater than 5.

>db.personData.createIndex(
{ age: 1, name: 1 },
{ partialFilterExpression: { age: { $gt: 25 } } }
)
We can specify a partialFilterExpression option for all
MongoDB index types.
Sparse Indexes
• The sparse property of an index ensures that the index
only contain entries for documents that have the
indexed field.
• The index is “sparse” because it does not include all
documents of a collection.
• The index skips documents that do not have the indexed
field.
• Subset of partial indexes
Create a Sparse Index
• To create a sparse index, use the db.collection.createIndex() method
with the sparse option set to true.

For example, the following operation in the mongo shell creates a sparse
index on the xmpp_id field of the addresses collection:

> db.addresses.createIndex( { "xmpp_id": 1 }, { sparse: true } )

The index does not index documents that do not include the xmpp_id
field.

NOTE:
Do not confuse sparse indexes in MongoDB with block-level indexes in
other databases. Think of them as dense indexes with a specific filter.
TTL Indexes

• TTL indexes are special indexes that MongoDB can use


to automatically remove documents from a collection
after a certain amount of time.
• This is ideal for certain types of information like
machine generated event data, logs, and session
information that only need to persist in a database for a
finite amount of time.
• Expire Data from Collections by Setting TTL for
implementation instructions.
Create a TTL index

• To create a TTL index, use the db.collection.createIndex()


method with the expireAfterSeconds option on a field
whose value is either a date or an array that contains date
values.

For example, to create a TTL index on the lastModifiedDate


field of the eventlog collection, use the following operation in
the mongo shell:

>db.eventlog.createIndex( { "lastModifiedDate": 1 }, {
expireAfterSeconds: 3600 } )
Hidden Indexes
• Hidden indexes are not visible to the query planner and cannot be
used to support a query.

• By hiding an index from the planner, users can evaluate the potential
impact of dropping an index without actually dropping the index.

• If the impact is negative, the user can unhide the index instead of
having to recreate a dropped index.

• And because indexes are fully maintained while hidden, the indexes
are immediately available for use once unhidden.

• Except for the _id index, you can hide any indexes.
Create a Hidden Index
• To create a hidden index, use the
db.collection.createIndex() method with the hidden
option set to true.

NOTE:
To use the hidden option with
db.collection.createIndex(), you must have
featureCompatibilityVersion set to 4.4 or greater.

However, once hidden, the index remains hidden even


with featureCompatibilityVersion set to 4.2 on
MongoDB 4.4 binaries.
For example, the following operation creates a hidden
ascending index on the borough field:

> db.addresses.createIndex(
{ borough: 1 },
{ hidden: true }
);
To verify, run db.collection.getIndexes() on
the addresses collection:
> db.addresses.getIndexes()
{
The operation returns the following
information: "v" : 2,
"key" : {
"borough" : 1
[
{ },
"name" : "borough_1",
"v" : 2,
"hidden" : true
"key" : {
}
"_id" : 1
]
},
"name" : "_id_" The index option hidden is only returned if
the value is true.
},
Thank you..

You might also like