这是我的模式:
/** Schemas */ var profile = Schema({ EmailAddress: String, FirstName: String, LastName: String, BusinessName: String }); var convSchema = Schema({ name: String, users: [{ type: Schema.Types.ObjectId, ref: 'Profiles' }], conversationType: { type: String, enum: ['single', 'group'], default: 'single' }, created: { type: Date, default: Date.now }, lastUpdated: { type: Date, default: Date.now } }); /** Models */ db.Profiles = mongoose.model('Profiles', profile); db.Conversations = mongoose.model('ChatConversations', convSchema); module.exports = db;然后,我尝试使用以下代码填充用户( mongoosejs/docs/populate. html ):
Then I try to populate Users using following code (mongoosejs/docs/populate.html):
db.Conversations.find(query).populate('users').exec(function (err, records) { console.log(records); });这将返回records但users数组作为空白数组[].
This is returning records but users array as a blank array [].
我也尝试了另一种方法( mongoosejs/docs/api.html#model_Model.populate ):
I also tried the other way around (mongoosejs/docs/api.html#model_Model.populate):
db.Conversations.find(query, function (err, records) { db.Conversations.populate(records, {path: "users", select: "BusinessName"}, function (err, records) { console.log(records); }); });结果相同.当我检查对概要文件收集记录的引用时.
Results are same. When I checked references into profile collection records are there.
有什么想法吗?
推荐答案我通过重命名模型(第三个观点)使它起作用:
I got it working by renaming model (the 3rd arguement):
mongoose.model( "Profiles", profile, "Profiles" );
问题是猫鼬在搜索profiles集合,但在数据库中以Profiles的形式出现.因此,我将其重命名为Profiles以匹配确切的名称.
The issue was Mongoose was searching for profiles collection but its there as Profiles in database. So I renamed it to Profiles to match the exact name.
Phewww!多亏我.
Phewww! Thanks to me.
更多推荐
猫鼬填充不与ObjectId数组一起使用
发布评论