mongodb中使用distinct去重的簡單方法
更新時間:2015年05月12日 10:11:32 投稿:hebedich
怎么在mongodb中實現(xiàn)類似于SQL中distinct的功能,查詢某一個字段所有的值,今天我們就來探討下這個問題。
MongoDB的destinct命令是獲取特定字段中不同值列表。該命令適用于普通字段,數(shù)組字段和數(shù)組內(nèi)嵌文檔.
mongodb的distinct的語句:
復制代碼 代碼如下:
db.users.distinct('last_name')
等同于 SQL 語句:
復制代碼 代碼如下:
select DISTINCT last_name from users
表示的是根據(jù)指定的字段返回不同的記錄集。
一個簡單的實例:
//
> 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 ]
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
相關(guān)文章
mongo數(shù)據(jù)集合屬性中存在點號(.)的解決方法
這篇文章主要給大家介紹了關(guān)于mongo數(shù)據(jù)集合屬性中存在點號(.)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10

