Indexes
Indexes are data structures that make it simple to navigate across the collection’s data set. They help to execute queries and find documents that match the query criteria without a collection scan.
These are the following different types of indexes in MongoDB:
6.1 Single field
MongoDB can traverse the indexes either in the ascending or descending order for single-field index
db.students.createIndex({“item”:1})
In this example, we are creating a single index on the item field and 1 here represents the filed is in ascending order.
A compound index in MongoDB contains multiple single filed indexes separated by a comma. MongoDB restricts the number of fields in a compound index to a maximum of 31.
db.students.createIndex({“item”: 1, “stock”:1})
Here, we create a compound index on item: 1, stock:1
6.2 Multi-Key
When indexing a filed containing an array value, MongoDB creates separate index entries for each array component. MongoDB allows you to create multi-key indexes for arrays containing scalar values, including strings, numbers, and nested documents.
db.students.createIndex({<filed>: <1 or -1>})
6.3 Geo Spatial
Two geospatial indexes offered by MongoDB are called 2d indexes and 2d sphere indexes. These indexes allow us to query geospatial data. On this case, queries intended to locate data stored on a two-dimensional plane are supported by the 2d indexes. On the other hand, queries that are used to locate data stored in spherical geometry are supported by 2D sphere indexes.
6.4 Hashed
To maintain the entries with hashes of the values of the indexed field we use Hash Index. MongoDB supports hash based sharding and provides hashed indexes.
db.<collection>.createIndex( { item: “hashed” } )
MongoDB Architecture
MongoDB is a popular NoSQL document-oriented database management system, known for its flexibility, high performance, high availability, and multi-storage engines. The term NoSQL means non-relational. It means that MongoDB isn’t based on a table-like relational database structure. It is used by Adobe, Uber, IBM, and Google. In this article, we will delve into the MongoDB architecture, exploring its key components and how they work together.