MongoDB中的集合查询(获取一组文档中某个字段的不同值列表)
运用方法:DBCollection对象方法中的 distinct() 语句:
语句结构:distinct(key,[query])
key字符串,指定获取哪个字段的不同值;query:包含标准查询选项的对象,指定了要从哪个文档中获取不同的字段值
举例说明:如下是 inventory 集合的数据
{ "_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" ] }
> db.inventory.distinct(“dept”) //获取dept字段的不重复值