Reputation: 119
I have a problem with my nodejs Api I want mongoose to return me only the array of the document but not the whole document.
This are my Schemas:
//Require Mongoose
var mongoose = require('mongoose');
//Define a 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 the products
catId: {type: String, required: true},
ts: {type: Number, required: true}, // wehn was the product online
buyCount: {type: Number, required: true}, // baught how many times
description: {type: String}, //more about the 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 is the Function in the Model which should fetch the Array
module.exports.getProductsOfCategory = function (catId, callback) {
Category.find({'products.catId': catId}, callback)
};
last but not least this is what happens when the route is 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 is just a test",
"price": 9.99,
"_id": "5998dc6a0c864d397a5bfd3d",
"items": []
},
{
"title": "testprod2",
"catId": "5998dc248c28a03974272da4",
"ts": 1500642848744,
"buyCount": 0,
"description": "this is just a 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 i just want to have this:
"products": [
{
"title": "testprod2",
"catId": "5998dc248c28a03974272da4",
"ts": 1500642848744,
"buyCount": 0,
"description": "this is just a test",
"price": 9.99,
"_id": "5998dc6a0c864d397a5bfd3d",
"items": []
},
{
"title": "testprod2",
"catId": "5998dc248c28a03974272da4",
"ts": 1500642848744,
"buyCount": 0,
"description": "this is just a 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": []
}
]
Upvotes: 0
Views: 770
Reputation: 4869
Just select the specific field you want:
Category.find({'products.catId': catId}, 'products', callback)
or in your response, change
res.send(products)
to res.send(products[0].products)
Upvotes: 0
Reputation: 1554
write query to select specific fields
module.exports.getProductsOfCategory = function (catId, callback) {
Category.find({'products.catId': catId},['products'], callback)
};
Upvotes: 1