just_user
just_user

Reputation: 12059

nodejs, mongodb get array of only specified fields value

Is it possible to get an array of a specified fields value in mongodb?

Like instead of getting:

[{"color":"blue"},{"color":"red"}]

I would like to get:

["blue","red"]

Is this question clear enough?

Upvotes: 0

Views: 95

Answers (1)

levi
levi

Reputation: 25091

You can run array.map on the results array to achieve the desired result:

var colors = [{"color":"blue"},{"color":"red"}].map(function(obj){
    return obj.color;
});

Upvotes: 1

Related Questions