例はこちら
http://mongoosejs.com/docs/populate.html
次のコードを提供します
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
});
_creator は上記で次のように定義されています
_creator : { type: Schema.ObjectId, ref: 'Person' }
コードを次のように変更すると
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: {name: 'test'}
});
データをMongoDBにうまく挿入しているようです。
{ "title" : "A man who cooked Nintendo", "_creator" : { "name" : "test" }, "_id" : ObjectId("4fb7a55315c5f2de07000002"), "fans" : [ ] }
挿入前にエラーをキャッチするにはどうすればよいですか? ObjectId だけでなく、有効な Person に対応していることも確認したいと思います。