mongodb count num of distinct values per field/key

前端 未结 6 1937
小蘑菇
小蘑菇 2020-11-28 04:18

Is there a query for calculating how many distinct values a field contains in DB.

f.e I have a field for country and there are 8 types of country values (spain, engl

6条回答
  •  有刺的猬
    2020-11-28 04:48

    To find distinct in field_1 in collection but we want some WHERE condition too than we can do like following :

    db.your_collection_name.distinct('field_1', {WHERE condition here and it should return a document})

    So, find number distinct names from a collection where age > 25 will be like :

    db.your_collection_name.distinct('names', {'age': {"$gt": 25}})

    Hope it helps!

提交回复
热议问题