Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

$elemMatch를 실행할 때 MongoDB 인덱스가 작동하지 않습니까?

<시간/>

$elemMatch로 인덱스를 올바르게 구현하려면 Explain() 개념을 사용해야 합니다. 먼저 문서로 컬렉션을 만들어 보겠습니다. −

> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1},{ sparse :true, background :true } );{ "createdCollectionAutomatically" :true, "numIndexesBefore" :1, "numIndexesAfter" :2, "확인":1}> db.workingOfIndexesDemo.insertOne({"정보":{"StudentDetails":{"StudentName":"Chris"}}});{ "승인됨":true, "insertedId":ObjectId ("5e06f94825ddae1f53b621f7")}> db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}});{ "승인됨":true, "insertedId":개체 ID 5e06f94f25ddae1f53b621f8")}> db.workingOfIndexesDemo.insertOne({"정보":{"StudentDetails":{"StudentName":"마이크"}}});{ "승인됨":true, "insertedId"53e06df3:ObjectId5 )}

다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -

> db.workingOfIndexesDemo.find();

이것은 다음과 같은 출력을 생성합니다 -

{ "_id" :ObjectId("5e06f94825ddae1f53b621f7"), "Information" :{ "StudentDetails" :{ "StudentName" :"Chris" } } }{ "_id" :ObjectId("5e06f94f25ddae1f5 :{ "StudentDetails" :{ "StudentName" :"David" } } }{ "_id" :ObjectId("5e06f95325ddae1f53b621f9"), "Information" :{ "StudentDetails" :{ "StudentName" :"Mike" } } 

다음은 MongoDB에서 Explain()을 사용하여 $elemMatch를 실행하는 쿼리입니다 -

> db.workingOfIndexesDemo.find({"Information.StudentDetails":{ $elemMatch:{ "StudentName" :"David"} } } ).explain();

이것은 다음과 같은 출력을 생성합니다 -

{ "queryPlanner" :{ "plannerVersion" :1, "namespace" :"test.workingOfIndexesDemo", "indexFilterSet" :false, "parsedQuery" :{ "Information.StudentDetails" :{ "$elemMatch" :{ " StudentName" :{ "$eq" :"David" } } } }, "winningPlan" :{ "stage" :"FETCH", "filter" :{ "Information.StudentDetails" :{ "$elemMatch" :{ "StudentName " :{ "$eq" :"데이비드" } } } }, "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" :{ "호스트" :"DESKTOP-QN2RB3H", "포트" :27017, "버전":"4.0.5", "gitVersion":"3739429dd92b92d1b0ab120911a23d50bf03c412" }, "확인":1}