Get distinct records values

Spread the love

Question Description

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 }

Query : find all with NOT_THE_SAME(name)

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.

Practice As Follows

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

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

Not what you're looking for? Browse other questions tagged

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.