Hello i'm learning mongodb and nodejs. i was successful in retrieving all collections of an order. Below is the output
The orders: { __v: 0,
nonmeat: 'pineapple',
meat: 'Bacon',
sauce: 'AlfredoSauce',
crust: 'medium',
_id: 571c1b9aed52d5e5462b99f9 },{ __v: 0,
nonmeat: 'pineapple',
meat: 'Bacon',
sauce: 'AlfredoSauce',
crust: 'medium',
_id: 571c1bba07f035e646d24aa8 },{ __v: 0,
nonmeat: 'parmesan,olives',
meat: 'Salami',
sauce: 'MarinaraSauce',
crust: 'large',
_id: 571c1e3c28dd30ed469eb6df },{ __v: 0,
nonmeat: 'parmesan,olives',
meat: 'Salami',
sauce: 'MarinaraSauce',
crust: 'large',
_id: 571c1f11ba02d9f646f4c5c0 }
But i want to convert this to a javascript array cause the collection returns an array of objects. I want to filter out __v
and _id
. I intend to use this result to be displayed on google pie chart. I just want the key items: nonmeat
, meat
, sauce
and crust
. I intend to display a percentage of the items on google pie chart.
Below is my code retreiving the data from mongodb:
app.get('/orders', function(req, res){
var allorders = [];
//get all orders
PizzaOrder.find({}, function(err, orders){
if(err) throw err;
console.log('The orders '+orders.length);
//this is wrong
for(var i=0; i<orders.length;i++) {
allorders.push(JSON.parse(orders[i]));
}
});
console.log('Orders retrieved '+ allorders);
res.json(allorders);
});
UPDATE!!!!!!!!
I was able to send it in json and view on the browser. This is my code:
app.get('/orders', function(req, res){
//get all orders
PizzaOrder.find({}, function(err, orders){
if(err) throw err;
console.log('The orders '+orders.length);
res.send(orders);
});
});
But i want to filter it out the _id:
and __v:
. I'm using ajax call to receive the response and intend to display on google pie chart.