I am using a query with a projection to retrieve only specific fields from a MongoDB collection. Is it required to output the results of such a query to an Array? I wasn't able to find any working example without this conversion to an array.
db.collection("collection").find({}, { projection: { Name : 1, Price : 1 } }).toArray( (err, result) => {
if (result) {
console.log(result)
}
})
However, by outputting the results to an Array, I am obtaining the result in the following format (a JSON formatted output within an array of 1):
[ {key1:value1, key2:value1}, {key1:value2, key2:value2}, ...]
I would like the results to be outputted as JSON.