翻译或纠错本页面 Show
Definition¶db.collection.distinct(field, query, options)¶Finds the distinct values for a specified field across a single collection and returns the results in an array.
The db.collection.distinct() method provides a wrapper around the distinct command. Results must not be larger than the maximum BSON size. Options¶{ collation: <document> }
Behavior¶Array Fields¶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. Index Use¶When possible, db.collection.distinct() operations can use indexes. Indexes can also cover db.collection.distinct() operations. See Covered Query for more information on queries covered by indexes. Examples¶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: 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: 参见 Dot Notation 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: For information on distinct() and array fields, see the Behavior 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: Specify a Collation¶3.4 新版功能. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. A collection myColl has the following documents: { _id: 1, category: "café", status: "A" } { _id: 2, category: "cafe", status: "a" } { _id: 3, category: "cafE", status: "a" } The following aggregation operation includes the Collation option: db.myColl.distinct( "category", {}, { collation: { locale: "fr", strength: 1 } } ) For descriptions on the collation fields, see Collation Document. How can I get distinct values from a field in MongoDB?In MongoDB, the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. It takes three parameters first one is the field for which to return distinct values and the others are optional.
Can we use distinct in MongoDB?To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.
What does distinct do in MongoDB?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.
What is aggregation in MongoDB?What is Aggregation in MongoDB? Aggregation is a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.
|