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

キーがMongoDBの数値である場合、サブドキュメントの値にアクセスするにはどうすればよいですか?


サブドキュメントの値にアクセスするには、最初にドキュメントを含むコレクションを作成しましょう-

> db.accessSubDocumentDemo.insertOne(
...    {
...
...       "Details" : {
...          "1" : {
...             "StudentLowerScore" : "33",
...             "StudentHoghScore" : "55"
...          },
...          "2" : {
...             "StudentLowerScore" : "45",
...             "StudentHoghScore" : "65"
...          },
...          "3" : {
...             "StudentLowerScore" : "39",
...             "StudentHoghScore" : "91"
...          },
...          "4" : {
...             "StudentLowerScore" : "41",
...             "StudentHoghScore" : "85"
...          }
...       }
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cd3baf0edc6604c74817cd6")
}

以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-

> db.accessSubDocumentDemo.find().pretty();

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

{
   "_id" : ObjectId("5cd3baf0edc6604c74817cd6"),
   "Details" : {
      "1" : {
         "StudentLowerScore" : "33",
         "StudentHoghScore" : "55"
      },
      "2" : {
         "StudentLowerScore" : "45",
         "StudentHoghScore" : "65"
      },
      "3" : {
         "StudentLowerScore" : "39",
         "StudentHoghScore" : "91"
      },
      "4" : {
         "StudentLowerScore" : "41",
         "StudentHoghScore" : "85"
      }
   }
}

ここで、キーが数字の場合にサブドキュメントの値にアクセスします。ここでは、数字1のキーのサブドキュメントにアクセスします-

> db.accessSubDocumentDemo.findOne().Details["1"];

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

{ "StudentLowerScore" : "33", "StudentHoghScore" : "55" }

  1. C#のStringDictionaryのキーと値のペアの数を取得します

    StringDictionaryのキーと値のペアの数を取得するには、コードは次のとおりです- 例 using System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main(){       StringDictionary strDict = new StringDictionary();       strDict.Add("1&

  2. Redis HLEN –ハッシュ値に含まれるフィールドの数を取得する方法

    このチュートリアルでは、コマンド– HLEN を使用して、キーに格納されているハッシュ値に含まれるフィールドの数を取得する方法について学習します。 redis-cliで。 redis HLENコマンドの構文は次のとおりです:- 構文:- redis host:post> HLEN <key name> 出力:- - (integer) value, representing the number of fields in the hash.- 0, if key does not exist.- Error, if key exist and value store