2017-03-02 6 views
0

私はnode.jsのmongooseを介して文書全体からサブ文書のみを取得しようとしています。次のように 私のデータベーススキーマは次のとおりです。mongooseのドキュメントクエリからサブ文書のみを取得する方法

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
// var Items = require('./items.js'); 

var Items = new Schema({ 
    no_of_times_ordered:Number, 
    item_name:String, 
    item_tag:String, 
    item_category:String, 
    item_stock:Number, 
    item_price:Number, 
    item_img:String, 
    item_illustrations:[String], 
    item_liked:Boolean, 
    no_of_likes:Number 
},{ versionKey: false }); 


var FoodTruckSchema = new Schema({ 
    foodtruck_name:String, 
    foodtruck_location:String, 
    foodtruck_rating:Number, 
    foodtruck_tag:String, 
    foodtruck_timing:String, 
    foodtruck_cusine:String, 
    foodtruck_img:String, 
    foodtruck_logo:String, 
    item_list: [Items] 
},{ versionKey: false }); 



module.exports = mongoose.model('foodtruck',FoodTruckSchema); 

下記のようクエリは次のとおりです。

var popularitems = function(req,res) { 
    foodtr.find(function(err,items){ 
     if (err) res.send(err); 
     res.json({ 
      status : '200', 
      message:'popular items list', 
      data: items 
     }); 
    }); 

だから私はすでにfoodtrucksを持つアイテムが取得されたクエリを入れているが、どのように私だけでアイテムを持つことができます応答?

+0

今まで試した質問を表示しますか? –

+0

更新された質問に試したクエリを入れました –

答えて

0

検索クエリでselectまたはprojectionを使用すると、itemsのみを取得できます。

これを試してみてください:

foodtr.find({},{'item_list':1},function(err,items){ 
    if (err) res.send(err); 
    res.json({ 
     status : '200', 
     message:'popular items list', 
     data: items 
    }); 
}); 

{}が、それはすべての文書を検索します。この場合には、状態を照会することと似ています。 {'item_list':1}は、mongoにitem_list配列のみを取得するように指示することです(_idは他のものと一緒にデフォルトで入ります)。

希望はこれです。

関連する問題