MongoDB: db.collection.distinct() method
db.collection.dataSize
The db.collection.distinct() method is used to find the distinct values for a specified field across a single collection and returns the results in an array.
Syntax:
db.collection.distinct(field, query)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
field | The field for which to return distinct values. | Required | string |
query | A query that specifies the documents from which to retrieve the distinct values. | Required | document |
Sample document in the restaurants collection:
{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}
............
Example: Return Distinct Values for a Field
The following example returns the distinct values for the field cuisine from all documents in the restaurants collection:
db.restaurants.distinct( "cuisine" );
Output:
The method returns the following array of distinct cuisine values:
[ "Irish", "Bakery", "American ", "Hamburgers", "Jewish/Kosher", "Delicatessen", "Ice Cream, Gelato, Yogurt, Ices", "Chinese", "Other", "Chicken", "Turkish", "Caribbean", "Donuts", "Sandwiches/Salads/Mixed Buffet", "Bagels/Pretzels", "Pizza", "Italian", "Steak", "Continental", "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "Polish", "German", "French", "Pizza/Italian", "Mexican", "Spanish", "Café/Coffee/Tea", "Tex-Mex", "Pancakes/Waffles", "Soul Food", "Seafood", "Hotdogs", "Greek", "Not Listed/Not Applicable", "African", "Japanese", "Indian", "Armenian", "Thai", "Chinese/Cuban", "Mediterranean", "Korean", "Bottled beverages, including water, sodas, juices, etc.", "Russian", "Eastern European", "Middle Eastern", "Asian", "Ethiopian", "Vegetarian", "Barbecue", "Egyptian", "English", "Sandwiches", "Portuguese", "Indonesian", "Chinese/Japanese", "Filipino", "Juice, Smoothies, Fruit Salads", "Brazilian", "Afghan", "Vietnamese/Cambodian/Malaysia", "Café/Coffee/Tea", "Soups & Sandwiches", "Tapas", "Moroccan", "Pakistani", "Peruvian", "Bangladeshi", "Czech", "Salads", "Creole", "Fruits/Vegetables", "Iranian", "Cajun", "Scandinavian", "Polynesian", "Soups", "Australian", "Hotdogs/Pretzels", "Southwestern", "Nuts/Confectionary", "Hawaiian", "Creole/Cajun", "Californian", "Chilean" ]
Example: Return Distinct Values for an Embedded Field
The following example returns the distinct values for the field zipcode, embedded in the address field, from all documents in the restaurants collection:
db.restaurants.distinct( "address.zipcode" );
Output:
The method returns the following array of distinct zipcode values:
[ "10019", "10462", "11224", "11225", "11374", "11369", "11219", "11234", "10460", "11226", "11214", "11208", "10065", "11223", "11414", "11218", "11004", "11209", "11215", "10021", "11356", "10314", "10003", "11106", "10024", "10025", "10012", "10005", "11368", "11203", "11205", "10466", "10007", "11201", "10018", "10474", "10174", "11418", "11379", "10016", "10022", "11236", "11385", "11212", "10023", "11230", "11371", "10306", "10458", "10308", "10038", "11211", "10028", "10017", "10465", "10014", "11222", "10013", "11101", "10009", "10305", "11235", "10011", "11363", "11232", "11103", "10309", "10032", "11228", "10036", "10033", "10451", "11372", "11366", "11204", "10312", "10001", "10467", "11435", "11432", "11001", "10302", "11229", "11694", "11420", "11697", "11367", "10463", "11375", "10006", "10452", "11427", "10475", "11373", "10310", "10304", "11238", "10010", "11105", "11377", "10034", "11360", "11415", "11416", "10470", "11354", "10301", "10002", "11361", "11358", "11213", "10128", "10464", "10004", "11239", "11104", "11231", "10075", "11364", "11237", "11249", "11362", "11357", "10303", "11434", "10472", "11220", "11378", "10461", "11417", "10027", "11365", "11413", "10029", "10459", "11421", "10468", "11217", "11216", "10456", "11423", "11207", "11355", "10469", "11206", "10455", "10454", "11210", "10119", "10020", "10453", "11412", "11419", "11429", "11370", "10040", "11430", "11221", "11422", "11428", "11040", "11693", "11411", "10031", "11691", "10457", "11102", "10037", "10106", "10030", "10471", "10112", "11426", "10039", "10035", "11005", "10307", "11692", "10044", "10026", "10280", "11233", "10103", "10121", "10282", "10473", "10281", "11436", "10153", "11433", "10057", "11242", "10111", "10122", "10168", "10107", "10000", "11109", "10171", "11256", "10069", "10311", "11010", "11451", "07005", "10317", "10104", "", "10048", "10123", "11352", "10165", "10167" ]
Example: Specify Query with distinct
The following example returns the distinct values for the field zipcode, embedded in the address field, from the documents whose cuisine is equal to "Delicatessen":
db.restaurants.distinct( "address.zipcode" , { "cuisine" : "Delicatessen" } );
Output:
The method returns the following array of distinct zipcode values:
[ "11234", "11209", "11356", "10003", "11106", "10314", "11385", "10306", "10308", "10021", "10022", "10006", "10475", "10007", "10017", "10019", "11101", "11215", "10018", "10036", "11365", "10001", "10025", "11426", "11228", "11360", "10012", "11357", "11370", "10016", "10028", "10309", "11694", "10310", "11377", "10013", "10463", "11375", "10002", "11363", "11217", "11361", "10004", "10282", "11364", "10038", "11105", "10464", "11378", "10458", "11222", "11220", "10065", "11232", "11415", "11242", "11354", "11103", "10023", "10451", "10014", "11208", "10112", "11417", "10032", "11430", "11432", "10024", "10020", "11238", "10005", "10011", "11369", "10467", "11230", "11231", "10455", "10010", "11379", "11201", "11355", "11219", "11368", "10033", "11214", "11102", "10027", "10301", "10280", "11434", "11372", "10469", "11206", "10029", "11435", "10461", "10468", "10474", "11423", "10075", "11416", "10462", "10302", "11236", "10312", "11414", "11223", "10035", "11374", "11367", "10128", "11422", "10465", "11237", "11418", "10453", "11207" ]
Retrieve the restaurants data from here
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.
Index Use
When possible, db.collection.distinct() operations can use indexes.
Indexes can also cover db.collection.distinct() operations.
Previous:
db.collection.dataSize() method
Next:
db.collection.drop() method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/shell-methods/collection/db-collection-distinct.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics