E
E
Egor Zhivagin2017-03-15 12:17:30
JavaScript
Egor Zhivagin, 2017-03-15 12:17:30

Is it possible to select values ​​from MongoDB without repeats?

There is a collection with objects that have the city property, conditionally: There are a
"city" : "Анапа"
lot of objects, the names of cities are different, but repeated.
I want to make an array variable of these values ​​on the server. Instead: city = ['Анапа', 'Сочи', ...]
Write

city = function* () {
yield db.collection('collection_name').find({},{"_id": 0, "city": 1}).toArray();
}

Questions:
1 - I don't want to get whole objects, but only property values, actually, hence the parameters in find() , but I'm almost sure that this doesn't work. Which direction to look?
2 - Sorting. I didn’t find it on the Internet, can I sort / select in mongoDB so that the field values ​​\u200b\u200bare not repeated? Or is it better to do it manually after receiving the array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
emp1re, 2017-03-15
@Krasnodar_etc

Google - mongo aggregation
1. $project
2. $filter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question