本文介绍了猫鼬填充字段,没有ref选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有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中,我有创建者字段,现在有了ref选项,因为它可以是User1Schema或User2Schema的类型.
In ConversationSchema I have creator field whic has now ref option, because it can be type of User1Schema or User2Schema.
如何使用猫鼬填充创建者字段
How can I populate creator field using mongoose
推荐答案
Conversation.find().populate('creator', null, 'User2').exec(callback)
文档尚不清楚.即将更新.
The docs aren't clear. Will update soon.
此外,此语法在v3.6中会更简单.
Also, the syntax for this will be simpler in v3.6.
这篇关于猫鼬填充字段,没有ref选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!