- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can I query on a MongoDB index if my query contains the $or operator?
Yes, you can do that. First, you need to create an index and then use explain(). Let us first create a MongoDB index. Following is the query:
> db.indexOrQueryDemo.ensureIndex({"First":1});
This will produce the following output
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }
The query to create second index is as follows
> db.indexOrQueryDemo.ensureIndex({"Second":1});
This will produce the following output
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }
Following is the query for $or operator with indexes. We have used explain() as well here
> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain();
This will produce the following output
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.indexOrQueryDemo", "indexFilterSet" : false, "parsedQuery" : { "$or" : [ { "First" : { "$eq" : 1 } }, { "Second" : { "$eq" : 2 } } ] }, "winningPlan" : { "stage" : "SUBPLAN", "inputStage" : { "stage" : "FETCH", "inputStage" : { "stage" : "OR", "inputStages" : [ { "stage" : "IXSCAN", "keyPattern" : { "First" : 1 }, "indexName" : "First_1", "isMultiKey" : false, "multiKeyPaths" : { "First" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "First" : [ "[1.0, 1.0]" ] } }, { "stage" : "IXSCAN", "keyPattern" : { "Second" : 1 }, "indexName" : "Second_1", "isMultiKey" : false, "multiKeyPaths" : { "Second" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "Second" : [ "[2.0, 2.0]" ] } } ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }
- Related Articles
- MongoDB query to implement OR operator in find()
- MongoDB query on nth element (variable index) of subdocument array
- How to query MongoDB using the $ne operator?
- How to query with nand operator in MongoDB?
- How do I query a MongoDB collection?
- How can I enhance my select query to make it faster in MySQL?
- How to implement MongoDB $or query?
- MongoDB Query to combine AND & OR?
- MongoDB query to find on the basis of true or false values
- MongoDB query to update array object in index N?
- How can I avoid too many OR statements in a MySQL query?
- MySQL query to check if a string contains a word?
- How can I to know if my database MongoDB is 64 bits?
- Return query based on date in MongoDB?
- MongoDB query condition on comparing 2 fields?

Advertisements