db.collection.distinct()
MongoDB とドライバー
このページではmongosh
メソッドがドキュメントされています。 MongoDB ドライバーで同等のメソッドを確認するには、プログラミング言語の対応するページを参照してください。
定義
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このメソッドでは、次のパラメーターが使われます。
Parameter | タイプ | 説明 |
---|---|---|
| string | The field for which to return distinct values. |
| ドキュメント | A query that specifies the documents from which to retrieve the distinct values. |
| ドキュメント | Optional. A document that specifies the options. See オプション. |
注意
Results must not be larger than the maximum BSON size. If your results exceed the maximum
BSON size, use the aggregation pipeline to retrieve distinct
values using the $group
operator, as described in
Retrieve Distinct Values with the Aggregation Pipeline.
The following diagram shows an example
db.collection.distinct()
call.
オプション
{ collation: <document> }
フィールド | タイプ | 説明 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| ドキュメント | 任意。 操作に使用する照合を指定します。 照合を指定すると、大文字・小文字やアクセント記号など、文字列を比較するための言語独自のルールを指定できます。 照合オプションの構文は次のとおりです。
照合を指定する場合、 照合が指定されていなくても、コレクションにデフォルトの照合が設定されている場合( コレクションにも操作にも照合が指定されていない場合、MongoDB では以前のバージョンで使用されていた単純なバイナリ比較によって文字列が比較されます。 1 つの操作に複数の照合は指定できません。たとえば、フィールドごとに異なる照合を指定できません。また、ソートと検索を一度に実行する場合、検索とソートで別の照合を使用できません。 |
動作
In a シャーディングされたクラスター, the distinct
command may return
orphaned documents.
For time series collections, the
distinct
command can't make efficient use of indexes. Instead, use a
$group
aggregation to group documents by distinct values. For
details, see 時系列の制限.
配列フィールド
If the value of the specified field
is an array,
db.collection.distinct()
considers each element of the array
as a separate value.
For instance, if a field has as its value [ 1, [1], 1 ]
, then
db.collection.distinct()
considers 1
, [1]
, and 1
as separate values.
For an example, see Return Distinct Values for an Array Field.
インデックスの使用
When possible, db.collection.distinct()
operations can use indexes.
Indexes can also cover
db.collection.distinct()
operations. See カバード クエリ for more information
on queries covered by indexes.
トランザクション
トランザクション内で個別の操作を実行するには、以下を使用できます。
For unsharded collections, you can use the
db.collection.distinct()
method/thedistinct
command as well as the aggregation pipeline with the$group
stage.For sharded collections, you cannot use the
db.collection.distinct()
method or thedistinct
command.To find the distinct values for a sharded collection, use the aggregation pipeline with the
$group
stage instead. See 個別の操作 for details.
重要
ほとんどの場合、分散トランザクションでは 1 つのドキュメントの書き込み (write) よりもパフォーマンス コストが高くなります。分散トランザクションの可用性は、効果的なスキーマ設計の代わりにはなりません。多くのシナリオにおいて、非正規化されたデータモデル(埋め込みドキュメントと配列)が引き続きデータやユースケースに最適です。つまり、多くのシナリオにおいて、データを適切にモデリングすることで、分散トランザクションの必要性を最小限に抑えることができます。
トランザクションの使用に関するその他の考慮事項(ランタイム制限や oplog サイズ制限など)については、「本番環境での考慮事項」も参照してください。
クライアントの切断
MongoDB 4.2以降では、 db.collection.distinct()
を発行したクライアントが操作の完了前に切断した場合、MongoDB は killOp
を使用して
db.collection.distinct()
を終了対象としてマークし 。
レプリカセット ノードの状態の制限
レプリカセット ノードでdistinct
操作を実行するには、ノードがPRIMARY
またはSECONDARY
状態である必要があります。 ノードがSTARTUP2
などの別の状態にある場合、操作はエラーになります。
クエリ設定
バージョン8.0の新機能。
クエリ設定を使用して、インデックスヒント、操作拒否フィルター、その他のフィールドを設定できます。設定はクラスター全体のクエリシェイプに適用されます。クラスターは、シャットダウン後も設定を保持します。
クエリオプティマイザは、クエリプランニング中の追加入力としてクエリ設定を使用します。これは、クエリを実行するために選択されたプランに影響します。クエリ設定を使用してクエリシェイプをブロックすることもできます。
クエリ設定を追加して例を調べるには、setQuerySettings
を参照してください。
find
、distinct
、aggregate
コマンドのクエリ設定を追加できます。
クエリ設定にはより多くの機能があり、廃止されたインデックスフィルターよりも優先されます。
クエリ設定を削除するには、 removeQuerySettings
を使用します。 クエリ設定を取得するには、集計パイプラインの$querySettings
ステージを使用します。
例
The examples use the inventory
collection that contains the
following documents:
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] } { "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] } { "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" } { "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
Return Distinct Values for a Field
The following example returns the distinct values for the field
dept
from all documents in the inventory
collection:
db.inventory.distinct( "dept" )
The method returns the following array of distinct dept
values:
[ "A", "B" ]
Return Distinct Values for an Embedded Field
The following example returns the distinct values for the field
sku
, embedded in the item
field, from all documents in the
inventory
collection:
db.inventory.distinct( "item.sku" )
The method returns the following array of distinct sku
values:
[ "111", "222", "333" ]
以下も参照してください。
ドット表記 for information on accessing fields within embedded documents
Return Distinct Values for an Array Field
The following example returns the distinct values for the field
sizes
from all documents in the inventory
collection:
db.inventory.distinct( "sizes" )
The method returns the following array of distinct sizes
values:
[ "M", "S", "L" ]
For information on distinct()
and array
fields, see the 動作 section.
Specify Query with distinct
The following example returns the distinct values for the field
sku
, embedded in the item
field, from the documents whose
dept
is equal to "A"
:
db.inventory.distinct( "item.sku", { dept: "A" } )
The method returns the following array of distinct sku
values:
[ "111", "333" ]
照合の指定
照合を指定すると、大文字・小文字やアクセント記号など、文字列を比較するための言語独自のルールを指定できます。
コレクション myColl
は、次のドキュメントを含みます。
{ _id: 1, category: "café", status: "A" } { _id: 2, category: "cafe", status: "a" } { _id: 3, category: "cafE", status: "a" }
下記の集計操作には 照合オプションが含まれます。
db.myColl.distinct( "category", {}, { collation: { locale: "fr", strength: 1 } } )
照合フィールドの説明については、照合ドキュメントを参照してください。