Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB indexes not working when executing $elemMatch?
To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −
> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1},{ sparse : true, background : true } );
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris"}}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e06f94825ddae1f53b621f7")
}
> db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e06f94f25ddae1f53b621f8")
}
> db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike"}}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e06f95325ddae1f53b621f9")
}
Following is the query to display all documents from a collection with the help of find() method −
> db.workingOfIndexesDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e06f94825ddae1f53b621f7"), "Information" : { "StudentDetails" : { "StudentName" : "Chris" } } }
{ "_id" : ObjectId("5e06f94f25ddae1f53b621f8"), "Information" : { "StudentDetails" : { "StudentName" : "David" } } }
{ "_id" : ObjectId("5e06f95325ddae1f53b621f9"), "Information" : { "StudentDetails" : { "StudentName" : "Mike" } } }
Following is the query to execute $elemMatch with explain() in MongoDB −
> db.workingOfIndexesDemo.find({"Information.StudentDetails": { $elemMatch: { "StudentName" : "David"} } } ).explain();
This will produce the following output −
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.workingOfIndexesDemo",
"indexFilterSet" : false,
"parsedQuery" : {
"Information.StudentDetails" : {
"$elemMatch" : {
"StudentName" : {
"$eq" : "David"
}
}
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"Information.StudentDetails" : {
"$elemMatch" : {
"StudentName" : {
"$eq" : "David"
}
}
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Information.StudentDetails.StudentName" : 1
},
"indexName" : "Information.StudentDetails.StudentName_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Information.StudentDetails.StudentName" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Information.StudentDetails.StudentName" : [
"[\"David\", \"David\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-QN2RB3H",
"port" : 27017,
"version" : "4.0.5",
"gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412"
},
"ok" : 1
}Advertisements