如何在Mongo获得独特的属性?

时间:2011-07-20 06:38:25

标签: mongodb distinct

我在Mongo有一个集合,其中的记录与此类似:

{ "id": NumberLong(141242), "name": "John Doe", "language": "en" }
{ "id": NumberLong(3556332), "name": "Mark Zuckerberg", "language": "jp" }
{ "id": NumberLong(124), "name": "Abigail Fidler", "language": "cn" }
{ "id": NumberLong(325262351), "name": "Joe Parker", "language": "jp" }
{ "id": NumberLong(98798), "name": "John Bush", "language": "en" }

我希望从该系列中选择所有独特的语言。

{ "language": "en" }
{ "language": "jp" }
{ "language": "cn" }

在Mongo中有什么办法吗?

1 个答案:

答案 0 :(得分:1)

MongoDB有一个distinct命令。

来自mongo shell的示例:

> db.mydb.distinct("language");
[ "en", "jp", "cn" ]