MongoDB 的 distinct 命令是獲取特定字段中不一樣值列表的最簡單工具。數組
該命令適用於普通字段、數組字段以及數組內嵌文檔(集合對象)。ide
db.getCollection('customer').distinct("customer_type") // chances字段的值是個集合,獲取集合內每一個對象的opportunitytype字段的值域 db.getCollection('customer').distinct("chances.opportunitytype")
統計某幾我的的共同好友:工具
db.person.distinct("myFriends", {"manName" : {"$in" : ["ZhenQin", "YangYan"]}})
使用 runCommand 命令來寫:spa
下面的示例返回 inventory 集合的 dept 字段的值域:對象
db.runCommand ( { distinct: "inventory", key: "dept" } )
下面的示例返回 inventory 集合中 dept 取值爲 "A" 的結果集中的 「item.sku」 字段的值域:文檔
db.runCommand ( { distinct: "inventory", key: "item.sku", query: { dept: "A"} } )