mongodb - Is it possible to return a list of specific values from a query? -


i have list of documents in "components" collection, each of has attribute "name". example:

{      "_id" : "ce83jfieojf8"      "name": "george" } 

i write query returns complete list of name values documents in components.

right now, can use db.components.find({'_id':0, 'name':1}), , return looks this:

{"name" : "george"} {"name" : "john"} {"name" : "paul"} 

and can name of individual 1 of documents, using index , .name attribute. if put in

db.components.find({'_id':0, 'name':1})[0].name  

i get:

george 

however, want query return:

george  john  paul 

i've looked through mongo documentation, when search 'returning multiple values' or 'returning lists', lists of documents, not lists of values documents.

is possible this, or need write separate function each of values individually?

thank assistance.

well let have following documents in collection:

{ "_id" : objectid("55a7cfeeeb68594275546c76"), "name" : "george" } { "_id" : objectid("55a7cfeeeb68594275546c77"), "name" : "john" } { "_id" : objectid("55a7cfeeeb68594275546c78"), "name" : "paul" } { "_id" : objectid("55a7cfeeeb68594275546c79"), "name" : "jack" } { "_id" : objectid("55a7cfeeeb68594275546c7a"), "name" : "paul" } 
  • you can use cursor.map collect , return names array
db.collection.find().map(function(u){ return u.name }) [ "george", "john", "paul", "jack", "paul" ] 
db.collection.distinct('name') [ "george", "john", "paul", "jack" ] 

Comments