The data structure in mongodb is
{'_id': 1, 'name': 'a'}
{'_id': 2, 'name': 'b'}
{'_id': 3, 'name': 'c'}
Can now pass
db.collection.find({}, {'name': 1})
The resulting list is:
[
{'name': 'a'},
{'name': 'b'},
{'name': 'c'}
]
Is there any way to get the following values (don't want to load the data into the memory recycle list for processing, does mongodb itself provide any method)
['a', 'b', 'c']
Mongodb supports js syntax, so use it directly.
map
Just do it// es6 db.collection.find({}, {'name': 1}).map( x => x.name ) // [ "a", "b", "c" ] db.collection.find({}, {'name': 1}).map(function(x) {return x.name} )
Please see map introduction.https://danmartensen.svbtle.com/javascripts-map-reduce-and-filter#map_1