Thursday, 15 April 2010

node.js - NodeJS / Mongoose Filter JSON -



node.js - NodeJS / Mongoose Filter JSON -

i building json api expressjs, nodejs , mongoose:

input -> id:

app.get('/folder/:id', function (req, res){ homecoming cars.find({reference: req.params.id}, function (err, product) { if (!err) { console.log(product); homecoming res.send(product); } else { homecoming console.log(err); } }); });

it shows json:

[{"_id":"b443u433","date":"2014-08-12","reference":"azerty","file":"087601.png"," ....:. {"_id":"hgf6789","date":"2013-09-11","reference":"azerty","file":"5678.pnf"," ...

i want display _id in json, when have lots of data.

how can that? filter?

you have iterate on "products" object obtain ids

something this: (disclaimer: haven't tested this)

app.get('/folder/:id', function (req, res){ homecoming cars.find({reference: req.params.id}, function (err, product) { if (!err) { console.log(product); var ids = new array(); for(var = 0; < product.length; i++){ ids.push(product[i]._id); } homecoming res.send(json.stringify(ids)); } else { homecoming console.log(err); } }); });

--edit

also, "products" may json string. may want parse before looping.

product = json.parse(product);

node.js mongodb express

No comments:

Post a Comment