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

MongoDBでサブドキュメントフィールド値の個別のリストを取得するにはどうすればよいですか?


サブドキュメントフィールド値の個別のリストを取得するには、dot(。)を使用できます。構文は次のとおりです-

db.yourCollectionName.distinct("yourOuterFieldName.yourInnerFieldName");

概念を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-

> db.getDistinctListOfSubDocumentFieldDemo.insertOne(
   ... {
      ... "StudentId": 101,
      ... "StudentPersonalDetails": [
         ... {
            ... "StudentName": "John",
            ... "StudentAge":24
         ... },
         ... {
            ... "StudentName": "Carol",
            ... "StudentAge":21
         ... }
      ... ]
   ... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90a9abb74d7cfe6392d7d8")
}
> db.getDistinctListOfSubDocumentFieldDemo.insertOne(
   ...
   ... {
      ... "StudentId": 102,
      ... "StudentPersonalDetails": [
         ... {
            ... "StudentName": "Carol",
            ... "StudentAge":26
         ... },
         ... {
            ... "StudentName": "Bob",
            ... "StudentAge":21
         ... }
      ... ]
   ... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90a9ceb74d7cfe6392d7d9")
}
> db.getDistinctListOfSubDocumentFieldDemo.insertOne(
   ... {
      ... "StudentId": 103,
      ... "StudentPersonalDetails": [
         ... {
            ... "StudentName": "Bob",
            ... "StudentAge":25
         ... },
         ... {
            ... "StudentName": "David",
            ... "StudentAge":24
         ... }
      ... ]
   ... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c90a9e6b74d7cfe6392d7da")
}

find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-

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

以下は出力です-

{
   "_id" : ObjectId("5c90a9abb74d7cfe6392d7d8"),
   "StudentId" : 101,
   "StudentPersonalDetails" : [
      {
         "StudentName" : "John",
         "StudentAge" : 24
      },
      {
         "StudentName" : "Carol",
         "StudentAge" : 21
      }
   ]
}
{
   "_id" : ObjectId("5c90a9ceb74d7cfe6392d7d9"),
   "StudentId" : 102,
   "StudentPersonalDetails" : [
      {
         "StudentName" : "Carol",
         "StudentAge" : 26
      },
      {
         "StudentName" : "Bob",
         "StudentAge" : 21
      }
   ]
}
{
   "_id" : ObjectId("5c90a9e6b74d7cfe6392d7da"),
   "StudentId" : 103,
   "StudentPersonalDetails" : [
      {
         "StudentName" : "Bob",
         "StudentAge" : 25
      },
      {
         "StudentName" : "David",
         "StudentAge" : 24
      }
   ]
}

サブドキュメントフィールド値の個別のリストを取得するためのクエリは次のとおりです-

> db.getDistinctListOfSubDocumentFieldDemo.distinct("StudentPersonalDetails.StudentName");

以下は出力です-

[ "Carol", "John", "Bob", "David" ]

  1. MongoDBのドキュメントフィールド値から値(TotalPrice – Discount)を差し引く方法は?

    ドキュメントフィールドの値から値を減算するには、MongoDBaggregate()で$subtractを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo599.insertOne({"TotalPrice":250,"DiscountPrice":35});{    "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db

  2. MongoDBで個別の要素の最大値を取得する方法

    個別の要素の最大値を取得するには、MongoDBのaggregate()で$sortと$groupを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo750.insertOne({id:101,value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102,va