db.collection.distinct()
获取字段去重值
语法
db.collection.distinct(<field>, <query>)说明
返回指定字段的所有不重复值(数组形式)。可传入查询条件过滤文档后再去重。
参数
field必填要去重的字段名
query过滤条件
示例
获取所有分类
获取 products 集合中所有不重复的 category 值
db.products.distinct("category")条件去重
获取活跃用户的城市列表
db.users.distinct("city", { status: "active" })