인덱스를 만든 다음 Explain()을 사용합니다. 문서로 컬렉션을 만들자 −
> db.demo278.ensureIndex({"Subjects":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo278.insertOne({"Subjects":["MySQL","MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e49096edd099650a5401a55") } > db.demo278.insertOne({"Subjects":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490978dd099650a5401a56") } > db.demo278.insertOne({"Subjects":["Spring","Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490984dd099650a5401a57") }
find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo278.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e49096edd099650a5401a55"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e490978dd099650a5401a56"), "Subjects" : [ "C", "C++" ] } { "_id" : ObjectId("5e490984dd099650a5401a57"), "Subjects" : [ "Spring", "Hibernate" ] }
다음은 성능을 확인하는 쿼리입니다 -
> db.demo278.find({Subjects:{$in:["Spring","MongoDB"]}}).explain();
이것은 다음과 같은 출력을 생성합니다 -
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo278", "indexFilterSet" : false, "parsedQuery" : { "Subjects" : { "$in" : [ "MongoDB", "Spring" ] } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "Subjects" : 1 }, "indexName" : "Subjects_1", "isMultiKey" : true, "multiKeyPaths" : { "Subjects" : [ "Subjects" ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "Subjects" : [ "[\"MongoDB\", \"MongoDB\"]", "[\"Spring\", \"Spring\"]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }