如何从nodejs mongoose中的模型关系访问另一个模型关系的访问权限

时间:2015-04-14 10:58:04

标签: node.js mongodb mongoose

我有3个模特。

1 creative.js:

var creativeSchema = new mongoose.Schema({
 title: {type: String, required: true, validate: lengthValidator },
 desc: {type: String },
 video_format: { type : mongoose.Schema.ObjectId, ref : 'VideoFormat' }
});     

return this.find(criteria)

 .populate('video_format')
 .populate('order')
 .sort({'createdDate': -1}) // sort by date
 .limit(options.perPage)
 .skip(options.perPage * options.page)
 .exec(cb);

2:order.js

var orderSchema = new mongoose.Schema({
 title: {type: String, required: true, validate: lengthValidator},
 desc: {type: String },
 creative: { type : mongoose.Schema.ObjectId, ref : 'Creative'}
}); 

  return this.find(criteria)
   .populate('creative') 
   .sort({'createdDate': -1}) // sort by date
   .limit(options.perPage)
   .skip(options.perPage * options.page)
   .exec(cb);

3:video_format.js

var videoFormatSchema = new mongoose.Schema({
 title: {type: String, required: true, validate: lengthValidator},
 creatives: [{ type : mongoose.Schema.ObjectId, ref : 'Order'}]
});


 return this.find(options)
 .sort({'createdDate': -1}) // sort by date
 .populate('creatives')
 .exec(cb);

所有人群都还可以。

我从广告素材访问video_format。

<%= creative.video_format.title %>

我从订单

访问广告素材
<%=  order.creative.title %>

但我没有从订单访问video_format。

*<%=  order.creative.video_format.title %>*

1 个答案:

答案 0 :(得分:0)

不幸的是,Mongoose只支持一个级别的人口。您可以尝试使用嵌套填充的第三方插件。

选中此项:https://github.com/buunguyen/mongoose-deep-populate