开发者

Select distinct rows from MongoDB

开发者 https://www.devze.com 2023-01-04 03:53 出处:网络
How do you selec开发者_Go百科t distinct records in MongoDB? This is a pretty basic db functionality I believe but I can\'t seem to find this anywhere else.

How do you selec开发者_Go百科t distinct records in MongoDB? This is a pretty basic db functionality I believe but I can't seem to find this anywhere else.

Suppose I have a table as follows

--------------------------
| Name    | Age          |
--------------------------
|John     | 12           |
|Ben      | 14           |
|Robert   | 14           |
|Ron      | 12           |
--------------------------

I would like to run something like SELECT DISTINCT age FROM names WHERE 1;


db.names.distinct('age')


Looks like there is a SQL mapping chart that I overlooked earlier.

Now is a good time to say that using a distinct selection isn't the best way to go around querying things. Either cache the list in another collection or keep your data set small.

0

精彩评论

暂无评论...
验证码 换一张
取 消