ここから例をとる
http://mongoosejs.com/docs/populate.html
「ファン」にも評価が含まれるように調整しようとすると
var db = require('houselib/db');
var Schema = db.Schema;
var mongoose = db.mongoose;
var PersonSchema = new Schema({
name : String
, age : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' }
, title : String
, fans : [{ type: Schema.ObjectId, ref: 'Person', rating: Number}]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
var aaron = new Person({ name: 'Aaron', age: 100 });
aaron.save(function (err) {
if (err) throw err;
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
, fans: [{type: aaron._id, rating: 4}]
});
story1.save(function (err) {
if (err) throw err;
Story
.find({ _creator: aaron._id })
.populate('_creator') // <-- not really necessary
.run(function (err, stories) {
if (err) throw err;
console.log('The stories JSON is an array: ', stories);
})
});
})
次のエラーが発生します
CastError:値"[オブジェクトオブジェクト]"の未定義へのキャストに失敗しました