我有一个看起来有点像的架构:
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);
https://stackoverflow.com/questions/10568281/
相关文章:
mongodb - 如何使用 nodejs 流式传输 MongoDB 查询结果?
mongodb - 无法使用 --db 创建备份 mongodump。身份验证失败
mongodb - 在 MongoDB shell 查询中获取 "data from collect
c# - 序列化 Mongo ObjectId 时出现 JSON.NET 转换错误
python - 文档中的 Mongoengine creation_time 属性
mongodb - 将 PouchDB 与 MongoDB 一起使用
mongodb - 异常 : can't convert from BSON type EOO to