InterviewSolution
| 1. |
How can we perform backup for a sharded cluster? |
|
Answer» Indexes help in improving the performance of queries. Without indexes, query must perform collection scan where each and every document of collection is scanned for the desired query result. With the use of proper indexes, we can LIMIT the number of documents scanned thus improving the performance of queries. Like collections indexes also use storage as they store a small portion of collection data. For example, if we create an index on field ‘NAME’ it will store data for this field and in ascending or descending order which also helps sort operations. Using indexes, we can satisfy equality matches and range-based queries more efficiently. Some of the different index options available for MongoDB are:
By default, MongoDB creates an index on the _id field at the time of creating an index. This is a unique index and PREVENTS applications from inserting multiple same values for the same _id field. MongoDB ensures that this index cannot be deleted.
These are indexes either on any one or combination of fields. i.e db.records.createIndex( { score: 1 } ) – Index on single field “score” db.products.createIndex( { "item": 1, "stock": 1 } ) – Index on comination of “item and stock”
MongoDB provides the option of creating an index on the contents STORED in arrays. For every element of the array, a separate index entry is created. We can select matching elements of the array using multikey indexes more efficiently.
MongoDB also provides a geospatial index which helps to efficiently query the geospatial coordinate data. 2d indexes for planar geometry and 2dsphere indexes for spherical geometry.
To support string content search in collection MongoDB provides text index. These indexes only store root words while ignoring the language-specific words like ‘the’, ‘a’ etc.
To search for specific filter expression in a collection partial indexes are used. Since they store only the subset of documents in a collection, they have lower storage requirements. Index creation maintenance and performance is also low for these indexes.
If we only want to get the fields of a document that are indexed and skip all other fields we can do so by using the sparse index.
Certain application has requirements where documents need to be removed AUTOMATICALLY after a certain amount of time. We can achieve this using TTL indexes. We specify TTL (time to live) for the documents after which a background process runs and removed these documents. This index is ideal for logs, session data and event data as such data only needs to persist for a limited time. |
|