我有一个看起来有点像的架构:
var conversationSchema = new Schema({
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
recipients: { type: [Schema.ObjectId], ref: 'User' },
messages: [ conversationMessageSchema ]
});
所以我的收件人集合,是一个引用我的用户架构/集合的对象 id 的集合。
我需要在查询中填充这些,所以我正在尝试这个:
Conversation.findOne({ _id: myConversationId})
.populate('user')
.run(function(err, conversation){
//do stuff
});
但显然“用户”没有填充...
有什么办法可以做到吗?
最佳答案
对于遇到此问题的其他人.. OP 的代码在架构定义中有错误.. 它应该是:
var conversationSchema = new Schema({
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
recipients: [{ type: Schema.ObjectId, ref: 'User' }],
messages: [ conversationMessageSchema ]
});
mongoose.model('Conversation', conversationSchema);
关于mongodb - Mongoose - 在 ObjectId 数组上使用 Populate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568281/