0

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?

1
  • How would you know what properties' values to extract as the array contains objects? Commented Apr 6, 2014 at 19:26

1 Answer 1

1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.