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.
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
The unique values are fetched when we use the distinct keyword. SELECT DISTINCT returns only distinct (different) values. DISTINCT eliminates duplicate records from the table. DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns.
You can use db.collection.distinct
to get back an array of unique values:
> db.test.distinct("name") [ "my_name", "john" ]
You can also use a distinct sentence with filtered collection. For example, you can get distinct values of names from salaries over 800 with the following query:
db.test.distinct("name", { "salary": { $gt: 800 } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With