INDEXING
MONGODB INDEXING
•With the use of Indexes, performing queries in MongoDB becomes more efficient.
•MongoDB would use these indexes to limit the number of documents that had to be
searched in the collection.
•Indexes are special data sets which store a partial part of the collection's data. Since
the data is partial, it becomes easier to read this data.
UNDERSTANDING IMPACT OF INDEXES
• Having too many indexes can slow down other operations such as the Insert, Delete
and Update operation.
• If there are frequent insert, delete and update operations carried out on documents,
then the indexes would need to change that often, which would just be an overhead
for the collection.
• An index can either be based on just one field in the collection, or it can be based on
multiple fields in the collection.
EXAMPLE
HOW TO CREATE INDEXES: CREATEINDEX()
MONGODB INDEX PROPERTIES
Unique Indexes
•This property of index causes MongoDB to reject duplicate values for the indexed
field.
• In other words, a unique property of indexes restricts it to insert the duplicate value
of an indexed field.
EXAMPLE
We create an index using createIndex() for the name field and set unique to be true.
Then insert a document with fields “name” and “city”.
db.student.createIndex({name:1},{unique:true})
PARTIAL INDEXES
•This property came in MongoDB version 3.2.
• Partial Indexes only index the documents that match the filter criteria.
•If we are creating an index with some conditions applied then it is a partial index.
SPARSE INDEXES
•The sparse property ensures that the index only contains entries for documents with
the indexed field.
•The index will skip the documents without the indexed field.
•We can combine this option with the unique index option in order to reject documents
with duplicate values for a field.
•And can ignore documents without an indexed key at the same time.
> db.student.createIndex({name:1},{unique:true,sparese:true})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 2,
“numIndexesAfter” : 3,
“ok” : 1
}
>

MongoDB - Indexing- Types iNDEXING.pptx

  • 1.
  • 2.
    MONGODB INDEXING •With theuse of Indexes, performing queries in MongoDB becomes more efficient. •MongoDB would use these indexes to limit the number of documents that had to be searched in the collection. •Indexes are special data sets which store a partial part of the collection's data. Since the data is partial, it becomes easier to read this data.
  • 3.
    UNDERSTANDING IMPACT OFINDEXES • Having too many indexes can slow down other operations such as the Insert, Delete and Update operation. • If there are frequent insert, delete and update operations carried out on documents, then the indexes would need to change that often, which would just be an overhead for the collection. • An index can either be based on just one field in the collection, or it can be based on multiple fields in the collection.
  • 4.
  • 5.
    HOW TO CREATEINDEXES: CREATEINDEX()
  • 13.
    MONGODB INDEX PROPERTIES UniqueIndexes •This property of index causes MongoDB to reject duplicate values for the indexed field. • In other words, a unique property of indexes restricts it to insert the duplicate value of an indexed field.
  • 14.
    EXAMPLE We create anindex using createIndex() for the name field and set unique to be true. Then insert a document with fields “name” and “city”.
  • 16.
  • 18.
    PARTIAL INDEXES •This propertycame in MongoDB version 3.2. • Partial Indexes only index the documents that match the filter criteria. •If we are creating an index with some conditions applied then it is a partial index.
  • 20.
    SPARSE INDEXES •The sparseproperty ensures that the index only contains entries for documents with the indexed field. •The index will skip the documents without the indexed field. •We can combine this option with the unique index option in order to reject documents with duplicate values for a field. •And can ignore documents without an indexed key at the same time.
  • 21.
    > db.student.createIndex({name:1},{unique:true,sparese:true}) { “createdCollectionAutomatically” :false, “numIndexesBefore” : 2, “numIndexesAfter” : 3, “ok” : 1 } >