node.js - nodejs/Mongoose - Only return the Array (with all elements) of the document -
i have problem nodejs api want mongoose return me array of document not whole document.
this schemas:
//require mongoose var mongoose = require('mongoose'); //define schema var schema = mongoose.schema; var replaceschema = new schema({ ts: {type: number, required: true}, by: {type: string, required: true} }) var itemschema = new schema({ title: {type: string, required: true}, item: {type: string, required: true}, ts: {type: string, required: true}, replaced: [replaceschema] }) var productschema = new schema({ title: {type: string, required: true, trim: true}, //name of products catid: {type: string, required: true}, ts: {type: number, required: true}, // wehn product online buycount: {type: number, required: true}, // baught how many times description: {type: string}, //more product price: {type: number, required: true}, // how much? items: [itemschema] }); var categoryschema = new schema({ name: {type: string, required: true}, ts: {type: number, required: true}, products: [productschema] }); var category = mongoose.model('category', categoryschema, 'category'); module.exports = category;
this function in model should fetch array
module.exports.getproductsofcategory = function (catid, callback) { category.find({'products.catid': catid}, callback) };
last not least happens when route hit:
router.post('/productsofcategory', function (req, res) { var catid = req.body.id; console.log(catid) category.getproductsofcategory(catid, function (err, products) { if (err) { res.send(err) } else { res.send(products) } }) })
my problem better explained:
im getting this:
[ { "_id": "5998dc248c28a03974272da4", "ts": 1503190052760, "name": "testcat1", "__v": 4, "products": [ { "title": "testprod2", "catid": "5998dc248c28a03974272da4", "ts": 1500642848744, "buycount": 0, "description": "this test", "price": 9.99, "_id": "5998dc6a0c864d397a5bfd3d", "items": [] }, { "title": "testprod2", "catid": "5998dc248c28a03974272da4", "ts": 1500642848744, "buycount": 0, "description": "this test", "price": 9.99, "_id": "5998dc710c864d397a5bfd3e", "items": [] }, { "title": "testprodukt", "description": "<p>tsetnietsiseid</p>", "price": 14.99, "catid": "5998dc248c28a03974272da4", "ts": 1503237239393, "buycount": 0, "_id": "59999477a5b6e63e60733220", "items": [] }, { "title": "aaaaa", "description": "<p>asasasas</p>", "price": 12, "catid": "5998dc248c28a03974272da4", "ts": 1503237690437, "buycount": 0, "_id": "5999963a68a3d63ee65a8546", "items": [] } ] } ]
but want have this:
"products": [ { "title": "testprod2", "catid": "5998dc248c28a03974272da4", "ts": 1500642848744, "buycount": 0, "description": "this test", "price": 9.99, "_id": "5998dc6a0c864d397a5bfd3d", "items": [] }, { "title": "testprod2", "catid": "5998dc248c28a03974272da4", "ts": 1500642848744, "buycount": 0, "description": "this test", "price": 9.99, "_id": "5998dc710c864d397a5bfd3e", "items": [] }, { "title": "testprodukt", "description": "<p>tsetnietsiseid</p>", "price": 14.99, "catid": "5998dc248c28a03974272da4", "ts": 1503237239393, "buycount": 0, "_id": "59999477a5b6e63e60733220", "items": [] }, { "title": "aaaaa", "description": "<p>asasasas</p>", "price": 12, "catid": "5998dc248c28a03974272da4", "ts": 1503237690437, "buycount": 0, "_id": "5999963a68a3d63ee65a8546", "items": [] } ]
write query select specific fields
module.exports.getproductsofcategory = function (catid, callback) { category.find({'products.catid': catid},['products'], callback) };
Comments
Post a Comment