DB refは次の場合に動作します:
Element.
findOne({unit: unit_id,).
populate("unit").
exec(function(err, element) {
console.log(element)
}
それは表示されます
{
"unit": {
"id": "10251179680282633",
"__v": 0,
},
"_id": "50b92eec29921b2531000003",
"__v": 0,
"created_at": "2012-11-30T22:10:52.513Z"
}
しかし、私が新しいユニットを作成し、ユニットを私の要素に入れて保存すると、
unit = new Unit();
element = new Element()
element.unit = unit;
私がする時:
console.log(element)
それは返す:
{
"unit": "50b9377d29921b2531000004",
"_id": "50b9377d29921b2531000005",
"created_at": "2012-11-30T22:47:25.361Z"
}
これはユニットをIDではなくオブジェクトとして見たいので、私が期待するものではありません。
何かヒント?
回答:
回答№1は1これは期待されている
あなたの console.log(element)
例えば、 element
ObjectIDへの参照が含まれています
それはモンゴースです populate
それは賢い人たちに unit
あなたのコレクションに保存されているIDに基づいて自分のコレクションから element
資料。
このページで詳しく説明しています
http://mongoosejs.com/docs/populate.html