MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

MongoDB-db.collection.find()メソッドのクエリプランに関する情報を解釈します


クエリプランの詳細については、MongoDBのexplain()を使用してください。ドキュメントを使用してコレクションを作成しましょう-

> db.demo637.ensureIndex({ClientName:1});
{
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 2,
   "ok" : 1
}
> db.demo637.insert({ClientName:"John"});
WriteResult({ "nInserted" : 1 })
> db.demo637.insert({ClientName:"Bob"});
WriteResult({ "nInserted" : 1 })
> db.demo637.insert({ClientName:"Johnson"});
WriteResult({ "nInserted" : 1 })

find()メソッドを使用してコレクションからすべてのドキュメントを表示する-

> db.demo637.find();

これにより、次の出力が生成されます-

{ "_id" : ObjectId("5e9c26916c954c74be91e6db"), "ClientName" : "John" }
{ "_id" : ObjectId("5e9c26936c954c74be91e6dc"), "ClientName" : "Bob" }
{ "_id" : ObjectId("5e9c269d6c954c74be91e6dd"), "ClientName" : "Johnson" }

ケース1

これが正規表現のクエリです//−

> db.demo637.find({ClientName:/john/}).explain();

これにより、次の出力が生成されます-

{
   "queryPlanner" : {
      "plannerVersion" : 1,
      "namespace" : "test.demo637",
      "indexFilterSet" : false,
      "parsedQuery" : {
         "ClientName" : {
            "$regex" : "john"
      }
   },
   "winningPlan" : {
      "stage" : "FETCH",
     "inputStage" : {
      "stage" : "IXSCAN",
      "filter" : {
         "ClientName" : {
            "$regex" : "john"
         }
      },
      "keyPattern" : {
         "ClientName" : 1
      },
      "indexName" : "ClientName_1",
      "isMultiKey" : false,
      "multiKeyPaths" : {
         "ClientName" : [ ]
      },
      "isUnique" : false,
      "isSparse" : false,
      "isPartial" : false,
      "indexVersion" : 2,
      "direction" : "forward",
      "indexBounds" : {
         "ClientName" : [
            "[\"\", {})",
            "[/john/, /john/]"
         ]
      }
   }
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
   "host" : "DESKTOP-QN2RB3H",
   "port" : 27017,
   "version" : "4.0.5",
   "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412"
},
"ok" : 1
}

ケース2

以下は、正規表現/ ^ / −

のクエリです。
> db.demo637.find({ClientName:/^john/}).explain();

これにより、次の出力が生成されます-

{
   "queryPlanner" : {
      "plannerVersion" : 1,
      "namespace" : "test.demo637",
      "indexFilterSet" : false,
      "parsedQuery" : {
         "ClientName" : {
            "$regex" : "^john"
         }
      },
      "winningPlan" : {
         "stage" : "FETCH",
         "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
               "ClientName" : 1
            },
            "indexName" : "ClientName_1",
            "isMultiKey" : false,
            "multiKeyPaths" : {
               "ClientName" : [ ]
            },
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : {
               "ClientName" : [
                  "[\"john\", \"joho\")",
                  "[/^john/, /^john/]"
               ]
            }
         }
      },
      "rejectedPlans" : [ ]
   },
   "serverInfo" : {
      "host" : "DESKTOP-QN2RB3H",
      "port" : 27017,
      "version" : "4.0.5",
      "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412"
   },
   "ok" : 1
}

  1. ネストされたドキュメントのMongoDBfind()クエリ?

    ネストされたドキュメントから値をフェッチするには、ドット表記を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo591.insert([ ...    { "Name": "John", "Age": 23 }, ...    {"Name": "Carol", "Age": 26}, ...    { "Name": "Robert",

  2. フィールド値で「@email」のような文字列を検索するMongoDBクエリ

    MongoDB find()を使用して電子メール文字列を検索します。ドキュメントを使用してコレクションを作成しましょう- > db.demo727.insertOne({UserId:"John@email.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"