获取不同的记录值 [英] Get distinct records values

查看:46
本文介绍了获取不同的记录值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以查询某个字段中值不相同的对象?例如,我有记录:

Is there a way to query for objects with not same values on some field? For example I have Records:

{ id : 1, name : "my_name", salary : 1200 }
{ id : 2, name : "my_name", salary : 800 }
{ id : 3, name : "john", salary : 500 }

查询:使用NOT_THE_SAME(name)查找所有内容

Query : find all with NOT_THE_SAME(name)

我只想要ID为1和3的记录,因为我指定了我不希望字段name或2和3中具有相同值的记录,在这种情况下没关系.

I just want records with id 1 and 3 because I specified that I don't want records with same value in field name or 2 and 3, it does not matter in this situation.

推荐答案

您可以使用 db.collection.distinct 返回唯一值数组:

You can use db.collection.distinct to get back an array of unique values:

> db.test.distinct("name")
[ "my_name", "john" ]

这篇关于获取不同的记录值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆