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

MongoDBで重複のないレコード値を取得する方法|distinct()メソッドの使い方

MongoDBで特定フィールドの重複しない(ユニークな)レコード値を取得したい場合は、distinct()メソッドを使用します。この記事では、distinct()メソッドの基本的な構文から、実際のコレクションを使った実行例まで、順を追ってわかりやすく解説します。

distinct()メソッドの基本構文

指定したフィールドの重複しない値だけを配列として取得するには、以下の構文を使用します。

db.yourCollectionName.distinct("yourFieldName");

この構文では、コレクション名とフィールド名を指定するだけで、そのフィールドに存在するユニークな値が配列形式で返されます。

サンプルコレクションの作成

実際に動作を確認するために、ドキュメントを含むコレクションを作成しましょう。以下のクエリで「distinctRecordDemo」というコレクションに学生情報のドキュメントを挿入します。

> db.distinctRecordDemo.insertOne({"StudentId":1,"StudentName":"John","StudentAge":21});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a78299b97a65744c1b50")
}
> db.distinctRecordDemo.insertOne({"StudentId":2,"StudentName":"John","StudentAge":22});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a78b99b97a65744c1b51")
}
> db.distinctRecordDemo.insertOne({"StudentId":3,"StudentName":"Carol","StudentAge":21});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a79a99b97a65744c1b52")
}
> db.distinctRecordDemo.insertOne({"StudentId":4,"StudentName":"Carol","StudentAge":26});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a7a499b97a65744c1b53")
}
> db.distinctRecordDemo.insertOne({"StudentId":5,"StudentName":"Sam","StudentAge":24});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a7b499b97a65744c1b54")
}
> db.distinctRecordDemo.insertOne({"StudentId":6,"StudentName":"Mike","StudentAge":27});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a7c799b97a65744c1b55")
}
> db.distinctRecordDemo.insertOne({"StudentId":7,"StudentName":"Sam","StudentAge":28});
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5c77a7d399b97a65744c1b56")
}

この例では、7件のドキュメントを挿入しました。「John」と「Carol」、「Sam」はそれぞれ複数回登場しているため、distinct()メソッドの効果を確認するのに適したデータセットです。

登録したドキュメントの確認

find()メソッドを使用すると、コレクション内のすべてのドキュメントを表示できます。pretty()を付けることで、整形された読みやすい形式で出力されます。

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

実行結果は以下の通りです。

{
    "_id" : ObjectId("5c77a78299b97a65744c1b50"),
    "StudentId" : 1,
    "StudentName" : "John",
    "StudentAge" : 21
}
{
    "_id" : ObjectId("5c77a78b99b97a65744c1b51"),
    "StudentId" : 2,
    "StudentName" : "John",
    "StudentAge" : 22
}
{
    "_id" : ObjectId("5c77a79a99b97a65744c1b52"),
    "StudentId" : 3,
    "StudentName" : "Carol",
    "StudentAge" : 21
}
{
    "_id" : ObjectId("5c77a7a499b97a65744c1b53"),
    "StudentId" : 4,
    "StudentName" : "Carol",
    "StudentAge" : 26
}
{
    "_id" : ObjectId("5c77a7b499b97a65744c1b54"),
    "StudentId" : 5,
    "StudentName" : "Sam",
    "StudentAge" : 24
}
{
    "_id" : ObjectId("5c77a7c799b97a65744c1b55"),
    "StudentId" : 6,
    "StudentName" : "Mike",
    "StudentAge" : 27
}
{
    "_id" : ObjectId("5c77a7d399b97a65744c1b56"),
    "StudentId" : 7,
    "StudentName" : "Sam",
    "StudentAge" : 28
}

distinct()メソッドで重複しない値を取得する

それでは、実際にdistinct()メソッドを使って、各フィールドのユニークな値を取得してみましょう。

ケース1:StudentNameフィールドの場合

学生名(StudentName)の重複しない値を取得するには、以下のクエリを実行します。

> db.distinctRecordDemo.distinct("StudentName");

実行結果は以下の通りです。「John」「Carol」「Sam」「Mike」の4種類の名前のみが返され、重複分は除外されていることがわかります。

[ "John", "Carol", "Sam", "Mike" ]

ケース2:StudentAgeフィールドの場合

次に、年齢(StudentAge)の重複しない値を取得してみます。

> db.distinctRecordDemo.distinct("StudentAge");

実行結果は以下の通りです。数値型のフィールドでも同様に動作し、重複のない年齢のリストが返されます。

[ 21, 22, 26, 24, 27, 28 ]

条件を指定してdistinct()を実行する

distinct()メソッドは、第2引数にクエリ条件を渡すことで、絞り込みを行った上でユニークな値を取得することもできます。例えば、21歳以上の学生の名前だけを重複なく取得したい場合は、次のように記述します。

> db.distinctRecordDemo.distinct("StudentName", { "StudentAge": { $gte: 21 } });

このように、distinct()メソッドはシンプルながら柔軟な使い方ができるため、データ分析や集計処理の前段階として非常に便利です。

  1. MongoDBコレクションから一意の値(重複なし)を取得する方法【distinct()の使い方】

    MongoDBでコレクション内の重複データを除外し、一意の値だけを取得したい場合は、distinct()メソッドを使用します。distinct()は、指定したフィールドに含まれる重複しない値を1つのコレクション全体から検索し、その結果を配列として返します。サンプルコレクションの作成まず、insertOne()を使ってドキュメントを挿入し、コレクションを作成します。> db.demo704.insertOne({"LanguageCode":"hi"}); { "acknowledged" : true, &quo

  2. MongoDBでグループごとの最大値を取得する方法($sortと$groupの活用)

    MongoDBで、重複している個別の要素(グループ)ごとに最大値を取得したい場合、aggregate()メソッドと$sort・$groupステージを組み合わせることで実現できます。この記事では、実際のコード例を通じて、その手順をわかりやすく解説します。サンプルコレクションの作成まず、ドキュメントを含むコレクションを作成します。ここではdemo750というコレクションに、idとvalueフィールドを持つドキュメントを挿入します。> db.demo750.insertOne({id:101,value:50}); { acknowledged : true, inserted