2016-03-19 4 views
0

私はこのようになります2つのスキーマがあります。それらの間のモンゴースに住んでいるときの最初のアイテムを取得するには?

var SearchQueryItemsSchema = new mongoose.Schema({ 
    //... 
    'auction': [{'type': mongoose.Schema.Types.ObjectId, 'ref': 'auctions'}], 
    // ... 
}); 

module.exports = mongoose.model('search_query_items', SearchQueryItemsSchema); 


var AuctionsSchema = new mongoose.Schema({ 
    'id': mongoose.Schema.ObjectId, 
    // ... 
}); 

module.exports = mongoose.model('auctions', AuctionsSchema); 

は関係many-to-oneです。主なものは、search_query_items.find({'search_query': value}).populate('auction').exec(function(err, query)のようなデータベースからデータを取得しようとすると、query[0]['auction']の代わりにquery[0]['auction'][0]が得られます。ポピュレートするときに最初の要素を取得するのはどういうことですか?

答えて

2

あなたはこれを試すことができます:あなたがオークション

の配列であるあなたがオークションフィールド定義されたので、配列オブジェクトを持っ
var SearchQueryItemsSchema = new mongoose.Schema({ 
//... 
'auction': {'type': mongoose.Schema.Types.ObjectId, 'ref': 'auctions'}, // remove the square backet 
// ... 
}); 

関連する問題