Sails.js 是否有一种标准方法可以通过.findOne()扩展与Sails、Mongo和Waterline的关联?
我的模型包含与其他模型的关联,这些模型显然只是与其ObjectId一起存储的。我想知道的是,是否有办法传入扩展所有关联或特定关联集的选项 因此“项目模型”如下所示(示例):Sails.js 是否有一种标准方法可以通过.findOne()扩展与Sails、Mongo和Waterline的关联?,sails.js,waterline,sails-mongo,Sails.js,Waterline,Sails Mongo,我的模型包含与其他模型的关联,这些模型显然只是与其ObjectId一起存储的。我想知道的是,是否有办法传入扩展所有关联或特定关联集的选项 因此“项目模型”如下所示(示例): module.exports = { attributes: { name: { type: 'string', required: true }, description:{ type: 'string' }, project: {
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
description:{
type: 'string'
},
project: {
model: 'project',
required: true
}, ...
item.save(function(error, item) { ... })
当您这样做时:
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
description:{
type: 'string'
},
project: {
model: 'project',
required: true
}, ...
item.save(function(error, item) { ... })
它将自动展开“项”中包含的所有关联。但是,如果传入此选项,它将不会展开:
item.save({ populate: false }, function(error, item) { ... })
我想知道为什么“save”会自动扩展,我想知道是否有一种方法可以让“findOne”也自动扩展。我知道您不希望总是展开,因为它可能会占用大量内存,但在某些时候返回完全展开的对象可能会很有用。是否要填充所有关联的模型
Model
.findOne({name:''})
.populateAll()
.exec(
function(error,result){}
)
它将填充所有关联。如果只想填充一个,可以使用
Model
.findOne({name:''})
.populate('project')
.exec(
function(error,result){}
)
更多。您可以在“填充”中使用“选择”、“位置”和“限制”(适用于Mongo)
当然,您可以填充一些关联
Model
.findOne({name:''})
.populate('project')
.populate('owner')
.populate('othermodel')
.exec(
function(error,result){}
)