6
は、私は次のモデルを持っている想像:マングースは、複数のサブパスでパスを移入
# MODEL A
schemaA = mongoose.Schema
_bId:
type: mongoose.Schema.Types.ObjectId
ref: "B"
# MODEL B
schemaB = mongoose.Schema
_cId:
type: mongoose.Schema.Types.ObjectId
ref: "C"
_dId:
type: mongoose.Schema.Types.ObjectId
ref: "D"
# MODEL C
schemaC = mongoose.Schema
_eId:
type: mongoose.Schema.Types.ObjectId
ref: "E"
モデルDとEは、他のオブジェクト参照と利便性のためには、したがって、もはや表示されていませんがありません。
モデル "A"にすべての参照を設定するベストプラクティスは何ですか? 次のように現在、私は(私はかなり頻繁にそれを必要とするので、それはインスタンスメソッドである)、このタスクを解決:
schemaA.methods =
populateAll: (cb) ->
@
.populate
path: "_bId"
model: "B"
populate:
path: "_cId"
model: "C"
populate:
path: "_eId"
model: "E"
, (error) =>
return cb error, @ if error?
D.findById @._bId._dId
.exec (error, d) =>
return cb error, @ if error?
@._bId._dId = d
return cb error, @
これが再びで複数のパスを複数のパスを取り込むので、私はすべての参照を移入した唯一の方法であります異なるモデルでは非常に困難です。私はすでに以下のような解決策を試してみましたが、1つが想像できるように、それだけで、前の集団を上書きする:
@
.populate
path: "_bId"
model: "B"
populate:
path: "_cId"
model: "C"
populate:
path: "_eId"
model: "E"
.populate
path: "_bId"
model: "B"
populate:
path: "_dId"
model: "D"