2013-01-31 44 views
15

我有3个模式:猫鼬填充字段没有裁判选项

var User1Schema = new Schema({ 
    name: {type:String , trim: true, required: true }, 
    status: {type:Number, required: true, default:1} 
},{collection:"user_1"}); 

var User2Schema = new Schema({ 
    name: {type:String , trim: true, required: true }, 
    email: {type: String, required: true, index: {unique: true} }, 
    status: {type:Number, required: true, default:1} 
},{collection:"user_2"}); 

var ConversationSchema = new Schema({ 

    participants: [{user:{type: ObjectId, required: true}], 
    creator: {type: ObjectId, required: true}, 

    status: { type:Number, required: true, default:1 } 

}, { collection:"conversation" }); 

在ConversationSchema我有whic现已裁判选项创作者场,因为它可以是User1Schema或User2Schema的类型。

我如何使用猫鼬

+2

请参阅'mongoose-dbref'插件:https://github.com/goulash1971/mongoose-dbref – JohnnyHK

+0

下面是'mongoose-dbref'的更新分支https://github.com/apiaryio /猫鼬-DBREF。但是,如果可能的话,你应该尝试改变你的模式而不是分支它:http://stackoverflow.com/questions/7617002/dealing-with-schema-changes-in-mongoose – mjhm

回答

23
Conversation.find().populate('creator', null, 'User2').exec(callback) 

docs填充创建者字段都不清楚。将尽快更新。

另外,在v3.6中,这样的语法会更简单。

+0

这允许一个multiple-参考领域,我想象?例如,文档的'_author'可能是'User'ObjectId _or_它可能是'Organization' ObjectId? –

+2

是的,如果你知道哪个模型是必要的,你可以在查询时指定这个。 – aaronheckmann

+0

这是否也适用于其中路径指向ObjectId的混合类型字段? –