- Reference >
- JavaScript Methods >
- db.collection.distinct()
db.collection.distinct()¶
-
db.collection.distinct()¶ The
db.collection.distinct()method finds the distinct values for a specified field across a single collection and returns the results in an array. The method accepts the following argument:Parameters: - field (string) – Specifies the field for which to return the distinct values.
- query (document) – Specifies the selection
queryto determine the subset of documents from which to retrieve the distinct values.
Note
- The
db.collection.distinct()method provides a wrapper around thedistinctcommand. Results must not be larger than the maximum BSON size. - When possible to use covered indexes, the
db.collection.distinct()method will use an index to find the documents in the query as well as to return the data.
Consider the following examples of the
db.collection.distinct()method:Return an array of the distinct values of the field
ord_dtfrom all documents in theorderscollection:Return an array of the distinct values of the field
skuin the subdocumentitemfrom all documents in theorderscollection:Return an array of the distinct values of the field
ord_dtfrom the documents in theorderscollection where thepriceis greater than10: