I I had in mongodb colleciton documents like:
Documents in collection (each has name property):
[{ name: 'Alex', .. }, { name: 'Jane', .. } ... ]
How can I query db to get the result simply mapping the name property:
['Ale', 'Jane', ...]
?
I I had in mongodb colleciton documents like:
Documents in collection (each has name property):
[{ name: 'Alex', .. }, { name: 'Jane', .. } ... ]
How can I query db to get the result simply mapping the name property:
['Ale', 'Jane', ...]
?
What you would want to use is query projections.
Queries in MongoDB return all fields in all matching documents by default. To limit the amount of data that MongoDB sends to applications, include a projection in the queries.
For your example, your query would look something like this:
db.people.find( {}, { "name": 1 } )
This will extract all documents but only return the name attribute (and _id) for each one. To remove the _id attribute as well, you would have to explicitly specify that you don't want it:
db.people.find( {}, { "name": 1, "_id": 0 } )
In order to retrieve all of the names in one array, you could use a forEach function to assemble the data:
var names = [];
db.people.find( {}, { "name": 1, "_id": 0 } ).forEach( function( doc ){
names.push( doc.name );
});