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 }
Updated on: 2019-07-30T22:30:25+05:30

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements