mongodb distinct去重
mongodb distinct去重mongodb的distinct的语句:
db.users.distinct('last_name')
等同于 SQL 语句:
select DISTINCT last_name from users
表示的是根据指定的字段返回不同的记录集。
一个简单的实例:
//
> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 99701})
> // shell helper:
> db.addresses.distinct("zip-code");
[ 10010, 99701 ]
> // running as a command manually:
> 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 ]