V
V
Vitaly2018-07-19 17:16:09
NoSQL
Vitaly, 2018-07-19 17:16:09

How to get a list of unique existing values ​​in Mongo DB?

All the best! Started learning monga. I did not find an answer in Google, so I suspect that my request was not quite correct. That's what I mean.
There is a collection myCollection :

{"_id":"01", "city": "London", "number": "11", "isActive": "true"}
{"_id":"02", "city": "Paris", "number": "31", "isActive": "true"}
{"_id":"03", "city": "Moscow", "number": "1", "isActive": "false"}
{"_id":"04", "city": "Kiev", "number": "131", "isActive": "true"}
{"_id":"05", "city": "London", "number": "551", "isActive": "true"}
{"_id":"07", "city": "London", "number": "11", "isActive": "true"}
{"_id":"08", "city": "London", "number": "44", "isActive": "true"}
{"_id":"09", "city": "Paris", "number": "77", "isActive": "false"}
{"_id":"10", "city": "Kiev", "number": "77", "isActive": "true"}
{"_id":"11", "city": "Paris", "number": "22", "isActive": "false"}
{"_id":"12", "city": "Paris", "number": "8", "isActive": "true"}

How can I get only the names of cities from this without duplicates?
{"city":  "London" }
{"city":  "Paris"}
{"city":  "Moscow"}
{"city":  "Kiev"}

That is, to see only which cities are present
Or just an array with cities without duplicates:
["London", "Paris", "Moscow", "Kiev"]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shushpanio, 2018-07-19
@shushpanio

Distinct

V
Vitaliy, 2018-07-19
@Scorpiored88

Sorry, I think I found it :)
db.myCollection.distinct("city")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question