Item スキーマとコレクション、および List スキーマとコレクションがあります。
現在、リストの作成とは別にアイテムを作成しています...しかし、各アイテムはリスト objectId を参照しています。
var ListSchema = new Schema({
name : { type: String, required: true, trim: true }
, user : { type: Schema.ObjectId, ref: 'User', index: true }
, description : { type: String, trim: true }
});
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, description: { type: String, trim: true }
, list : { type: Schema.ObjectId, ref: 'List', index: true }
});
リストを取得して、そのリストのアイテムをそのインスタンスの上にロードできるかどうかを知りたいです。
だから私はできる:
list.items
ビュー内のすべてのアイテムを取得します。
//routes/list.js
List.find({ user: req.params.userid }, function(err, lists){
//i want to populate list.items here
res.render('/lists', { lists: lists });
});
//views/list.ejs
<% lists.forEach(function(list){ %>
List has <%= list.items.length %> items in it
<% }) %>