How to add only values of an objects from MongoDB to an array in NodeJS?

Viewed 47

So here's an example of what my collection looks like:

https://i.stack.imgur.com/E5tES.png

When I run the command:

db.Try.find({Type: "Electronic"}, {Brand: 1, _id: 0})

My result is:

[
  { Brand: 'Apple' }
  { Brand: 'Dell' }
  { Brand: 'Samsung' }
]

I want to put the above data into a NodeJS array so that I can display it in HTML form.

When I try my output is:

[
  { Brand: 'Apple' },
  { Brand: 'Dell' },
  { Brand: 'Samsung' }
]

But expected output is:

['Apple','Dell','Samsung']

How can I get those results??

1 Answers

You can map the output from MongoDB:

const mongodb_output = [
  { Brand: 'Apple' },
  { Brand: 'Dell' },
  { Brand: 'Samsung' }
];

const result = mongodb_output.map(item => item.Brand);
console.log(result);

Related