mongodb中使用distinct去重的简单方法
MongoDB的destinct命令是获取特定字段中不同值列表。该命令适用于普通字段,数组字段和数组内嵌文档.
mongodb的distinct的语句:
db.users.distinct('last_name')
等同于SQL语句:
selectDISTINCTlast_namefromusers
表示的是根据指定的字段返回不同的记录集。
一个简单的实例:
//
>db.addresses.insert({"zip-code":10010})
>db.addresses.insert({"zip-code":10010})
>db.addresses.insert({"zip-code":99701})
>//shellhelper:
>db.addresses.distinct("zip-code");
[10010,99701]
>//runningasacommandmanually:
>db.runCommand({distinct:'addresses',key:'zip-code'})
{"values":[10010,99701],"ok"
//
>db.comments.save({"user":{"points":25}})
>db.comments.save({"user":{"points":31}})
>db.comments.save({"user":{"points":25}})
>db.comments.distinct("user.points");
[25,31]
以上所述就是本文的全部内容了,希望大家能够喜欢。