1

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.

EI-01
  • 1,075
  • 3
  • 24
  • 42

2 Answers2

1

If your question is how to remove _id and __v simply do:

 for(var i=0; i<orders.length;i++) {
    delete orders[i].__v;
    delete orders[i]._id;
    allorders.push(orders[i]);
 }
Emilio Platzer
  • 2,327
  • 21
  • 29
0

try without JSON.parse

     //try this
     for(var i=0; i<orders.length;i++) {
        allorders.push(orders[i]);
     }
Emilio Platzer
  • 2,327
  • 21
  • 29
  • i updated my question. As i was able to send a json and view on browser but i wanter to filter out the `_id` and `__v` and use the items to display in google pie chart – EI-01 Apr 24 '16 at 02:15