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

쿼리에 $or 연산자가 포함된 경우 MongoDB 인덱스를 쿼리할 수 있습니까?

<시간/>

네, 그렇게 할 수 있습니다. 먼저 인덱스를 만든 다음 Explain()을 사용해야 합니다. 먼저 MongoDB 인덱스를 생성해 보겠습니다. 다음은 쿼리입니다.

> db.indexOrQueryDemo.ensureIndex({"First":1});

그러면 다음과 같은 출력이 생성됩니다.

{
   "createdCollectionAutomatically" : false,
   "numIndexesBefore" : 2,
   "numIndexesAfter" : 3,
   "ok" : 1
}

두 번째 인덱스를 생성하는 쿼리는 다음과 같습니다.

> db.indexOrQueryDemo.ensureIndex({"Second":1});

그러면 다음과 같은 출력이 생성됩니다.

{
   "createdCollectionAutomatically" : false,
   "numIndexesBefore" : 3,
   "numIndexesAfter" : 4,
   "ok" : 1
}

다음은 인덱스가 있는 $or 연산자에 대한 쿼리입니다. 여기에서도 Explain()을 사용했습니다.

> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain();

그러면 다음과 같은 출력이 생성됩니다.

{
   "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
}