如何从nodejs mongoose中的模型关系访问另一个模型关系的访问
How to access access of another model relationship from a model relationship in nodejs 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 %>*
遗憾的是,Mongoose 只支持一级人口。您可以尝试一些用于嵌套填充的第 3 方插件。
我有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 %>*
遗憾的是,Mongoose 只支持一级人口。您可以尝试一些用于嵌套填充的第 3 方插件。