Skip to content
+91-7795206615
|
info@habilelabs.io

  • Home
  • About
  • Services
    • Custom Application Development
    • UI/UX Designing
    • Web Application Development
    • Offshore Product Services
    • Technical Outsourcing
    • ERP Services
  • Company
    • Careers
    • Case Studies
  • Specialization
    • Frontend Frameworks
      • Angular
      • ReactJS
      • VueJS
      • HTML / CSS
      • Javascript /Jquery
      • Bootstrap
      • Material design
    • Backend Frameworks
      • NodeJS
      • Meteor
      • GraphQL
      • Loopback
      • Salesforce
      • Spring Boot
      • Odoo
      • Laravel
    • Database / ORM
      • MySQL
      • PostgreSQL
      • Oracle
      • MongoDB
      • Google Firebase
      • Mongoose
      • Sequelize
      • Hibernate / JPA
    • Languages
      • Java Script
      • Dot Net
      • Java
      • Python
      • C / C++
      • PHP
      • AI / ML
      • Type Script
    • Mobile Frameworks
      • Ionic
      • Native Script
      • Native Android App
      • Native iOS App
      • Google Flutter
      • React Native
  • Blog
  • Hire Us

Query Optimizer and Indexing in MongoDB

Categories

  • Angular
  • Business Strategies
  • Cloud Services
  • CRM
  • Design Pattern
  • E-commerce
  • ERP Applications
  • Javascript
  • Meteor
  • Mobile development
  • Mongo DB
  • Node JS
  • Odoo
  • Our Partners
  • PHP
  • React
  • SAAS
  • Salesforce
  • SAP
  • Selenium
  • Tech stack Migration
  • Testing
  • UI-UX Design
  • Uncategorized
  • VAPT
  • Visualforce
  • Web Development
  • Web Security

Categories

  • Angular
  • Business Strategies
  • Cloud Services
  • CRM
  • Design Pattern
  • E-commerce
  • ERP Applications
  • Javascript
  • Meteor
  • Mobile development
  • Mongo DB
  • Node JS
  • Odoo
  • Our Partners
  • PHP
  • React
  • SAAS
  • Salesforce
  • SAP
  • Selenium
  • Tech stack Migration
  • Testing
  • UI-UX Design
  • Uncategorized
  • VAPT
  • Visualforce
  • Web Development
  • Web Security
Query Optimizer and Indexing in MongoDB

What is indexing in MongoDB?

Basically, index is a part of data structure that helps to find or search data from the collection in an efficient manner. But MongoDB indexing eases this process and find the data by matching queries without performing whole collection scan.

If a collection is well indexed, then MongoDB uses the index to fetch data quickly because it uses an index and limits the amount of data to be examined.

MongoDB Indexing Types

Indexing in MongoDB has many types that help to execute data in an efficient way and optimize your query as well.
1. Single field index
2. Compound index
3. Multikey index
4. Geo index
5. Covered index
6. Hashed index

How to Create, Delete and get Index?

Simply run this command in your Mongo shell to create indexes-

1
Db.collection.createIndex({type : 1})

• Collection means your collection name
• Type means you filed name that you want to index
• 1 for ascending order and -1 for descending order

How to Delete indexes –

1
2
Db.collection.dropIndexes({type : 1}) For particular index delete
Db.collection.dropIndexes() Delete All indexes

How to Get All indexes –

1
Db.collection.getIndexes()

Index Properties

Indexing has many properties like-

• Unique – It makes that field Unique that helps MongoDB to reject duplicate field of the collection. Just pass this during index creation.

1
Db.collection.createIndex({type : 1},{unique:true})

• Sparse – A very useful property of index which ensures that the index takes only the data that have indexed field. Just pass this during index creation-

1
Db.collection.createIndex({type: 1},{ sparse:true})

• Partial – It is similar like sparse but in partial we just make a filter condition, so if the data will match this filter index condition then it will take if not then simply index ignore this indexed field.

Key Point To Remember-

Mongo DB uses only one index at a time. If you have more than one field in your query then you will have to use compound index instead of the single index. Compound index matters with ordering so the order of query filed same as compound index field order, then it works perfectly.

Also, MongoDB optimizes best index that is suitable for the query so you have to not worry about this. It will take the best index that matches the query.

For example, if this is your query- 

1
Db.find({_id:101,”type”:”user”})

Then both indexes not were using for finding data-

1
2
Db.createIndex({_id:1})
Db.createIndex({type:1}

So only compound index will use for finding the data fast-

1
Db.createIndex({_id:1,type:1})

• Indexing was fast the data searching but it will slow the Insert/Update/Delete so indexing fast the writing process and slow the Read process

• If you using sorting in your query to sort data then it affects to your query and also slows the query process so it takes more time.
Just use the sorting filed in your index at last then it will optimize your query very effective like this-

1
2
Db.createIndex({_id:1,type:1,date:-1})
Db.find({_id:101,”type”:”user”}).sort({date:-1})

• Use explain in your query to ensure that indexed is applied properly in winning plan-

1
Db.find({_id:101,”type”:”user”}).sort({date:-1}).explain(true)

I hope this article will help you understand the fast process of MongoDB and how it optimizes the search query with Index.

If you have any query related to Indexing in MongoDB then ask in a comment.

Hope you like this post, so don’t forget to share with friends on Facebook.

Posted bybalgopalMarch 31, 2017January 15, 2021Posted inMongo DBTags: Indexing in MongoDB, MongoDB Indxing, Query Optimizer

Post navigation


:

Leave a comment

Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • PyScript – Python for the Web Browser

    PyScript – Python for the Web Browser

  • Cross-Database with Association in Postgres with Sequelize

    Cross-Database with Association in Postgres with Sequelize

  • How can You Improve Your Communication Skills – Effective Tips

    How can You Improve Your Communication Skills – Effective Tips

  • 5 Reasons that Make CRM Software Beneficial for Every Business

    5 Reasons that Make CRM Software Beneficial for Every Business

  • Do Low-Code Solutions have a Future in Web Development?

    Do Low-Code Solutions have a Future in Web Development?

  • A Revenue Management Reset in Consumer Goods

    A Revenue Management Reset in Consumer Goods

Talk to our experts now

Have a project or looking for development team? Contact us.

Get a quote

About Us

Habilelabs Private Limited is the ISO 9001:2015 certified IT company, which has marked its flagship in 20+ countries with 100+ projects successfully

Company

  • About
  • Blog
  • Careers
  • Hire Us
  • Privacy Policy

Contact Us

  • +91-9828247415
  • +91-9887992695
  • info@habilelabs.io

Follow Us

Office

  • Habilelabs Private Limited
    4th Floor, I.G.M. School Campus,
    Sec-93 Agarwal Farm, Mansarovar,
    Jaipur, Rajasthan India
    Pin:302020