Wildcard Index in MongoDB 4.2

In this blog we can see about Wildcard Index which is introduced in MongoDB 4.2 version.

MongoDB 4.2 introduced Wildcard Indexes for supporting queries unknown or arbitrary fields. 

  • Wildcard Index filter that can automatically match any fields,sub documents or an array in a collection.
  • Wildcard Index supports most of all queries. It will reduce execution time and index size then automatically in our performance is good.

Experimental scenario :-

I have made one sample collection called University with sub-document.

> db.university.find().pretty()
{
"_id" : ObjectId("5d4fabda42f49077f8246bb2"),
"College_name" : "Annauniversity",
"Description" : {
"Department" : [
"Electronics",
"Electrical",
"Mechanical",
"Computer Science"
],
"Member_details" : {
"Total_members" : 572,
"Classes" : "14"
}
}
}

In university collection we have a Description field which contains a variable selection of other fields and values.

Now we want to find the classes which have a value of 12 in the Description field.

Simple find query :-

> db.university.find( { "Description.Member_details.Classes": 12 } )

Please find explain plan of the above mentioned query 

 > db.university.find( { "Description.Member_details.Classes": 12 } ).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.university",
"indexFilterSet" : false,
"parsedQuery" : {
"Description.Member_details.Classes" : {
"$eq" : 12
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"Description.Member_details.Classes" : {
"$eq" : 12
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},

Here that query is not using any index ,If we create Wildcard Index on Description field then query will use the index.

This is the command to create the Wildcard Index of Description field.

> db.university.createIndex( { "Description.$**" : 1 } );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

Now I have created Wildcard Index on Description field please find explain plan of after Index creation.

> db.university.find( { "Description.Member_details.Classes": 12 } ).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.university",
"indexFilterSet" : false,
"parsedQuery" : {
"Description.Member_details.Classes" : {
"$eq" : 12
}
},
"queryHash" : "EFFDC2E6",
"planCacheKey" : "6855A0B2",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
  "stage" : "IXSCAN",
"keyPattern" : {
"$_path" : 1,
"Description.Member_details.Classes" : 1
},
"indexName" : "Description.$**_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"$_path" : [ ],
"Description.Member_details.Classes" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"$_path" : [
"[\"Description.Member_details.Classes\", \"Description.Member_details.Classes\"]"
],
"Description.Member_details.Classes" : [
"[12.0, 12.0]"
]
}
}
},
"rejectedPlans" : [ ]
},

Note :- Here I have explained about very simple example.

Conclusion :-

I hope this blogs will help you to how Wildcard Index is creating ,access the query and reduce the query access time.

For more details please find link Wildcard Index MongoDB 4.2

Published by arunkumarblog

RHCE Linux RedHat Certified Professionals,Working on MongoDB and MySQL Database Administrator.

Leave a comment

Design a site like this with WordPress.com
Get started