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

MongoDBでネストされたオブジェクトを取得するにはどうすればよいですか?


MongoDBでネストされたオブジェクトを取得するには、$演算子を使用します。まず、ドキュメントを使用してコレクションを作成しましょう-

> db.queryNestedObject.insertOne(
...    {
...       "StudentName" : "James",
...       "StudentSubjectScore" : [
...          {"StudentMongoDBScore":98},
...          {"StudentCScore":92},
...          {"StudentJavaScore":91}
...       ]
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccf49a9dceb9a92e6aa1962")
}

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

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

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

{
   "_id" : ObjectId("5ccf49a9dceb9a92e6aa1962"),
   "StudentName" : "James",
   "StudentSubjectScore" : [
      {
         "StudentMongoDBScore" : 98
      },
      {
         "StudentCScore" : 92
      },
      {
         "StudentJavaScore" : 91
      }
   ]
}

以下は、ネストされたオブジェクトを取得するためのクエリです-

> db.queryNestedObject.find({'StudentSubjectScore.StudentJavaScore' : 91},{'StudentSubjectScore.$': 1 , _id: 0});

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

{ "StudentSubjectScore" : [ { "StudentJavaScore" : 91 } ] }

  1. MongoDBのネストされたオブジェクトに対するグループクエリ?

    このために、MongoDBで$groupと一緒にドット表記を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "Chris", ...          "Age":32,

  2. ネストされたPython辞書を作成する方法は?

    辞書オブジェクトは変更可能です。したがって、1つのディクショナリオブジェクトをキーの値コンポーネントとして使用できます。したがって、ネストされたディクショナリオブジェクトを作成できます。別のディクショナリオブジェクトは、キーに関連付けられた値として定義されます。 >>> students={"student1":{"name":"Raaj", "age":23, "subjects":["Phy", "Che", "maths&