2017-08-20 92 views
0

我的nodejs Api有問題Api 我想讓貓鼬只返回文檔數組而不是整個文檔。nodejs/Mongoose - 僅返回文檔的陣列(包含所有元素)

這是我的架構:

//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;

這是在型號的功能應該取陣列

module.exports.getProductsOfCategory = function (catId, callback) { 
 
    Category.find({'products.catId': catId}, callback) 
 
};

最後但並非最不重要的,這是當路線被擊中會發生什麼:

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) 
 
     } 
 
    }) 
 
})

我的問題更好地解釋:

即時得到這樣的:

[ 
 
    { 
 
     "_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": [] 
 
      } 
 
     ] 
 
    } 
 
]

但我只是希望能有這樣的:

"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": [] 
 
      } 
 
     ]

+0

現在即時得到只是這回:[ { 「_id」:「5998dc248c28a03974272da4」 } ]這是類別的id,其中pro在這個awnser和res.send(products [0] .products)的幫助下,管道保存了 – b0ss

回答

0

寫入查詢來選擇特定字段

module.exports.getProductsOfCategory = function (catId, callback) { 
    Category.find({'products.catId': catId},['products'], callback) 
}; 
+0

我解決了它。 – b0ss

0

只需選擇你想要的特定領域:

Category.find({'products.catId': catId}, 'products', callback) 

或在您的回覆中,ch安格

res.send(products)res.send(products[0].products)

0

這是給我正確的事情,但我想更多的 「格式化」,如:

  { 
 
       "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": [] 
 
      } 
 
    

+0

不要把評論作爲答案。如有必要,更新問題並提供你想要的結果 –

相關問題