【问题标题】:Mongoose "ref" documentation, cannot find猫鼬“参考”文档,找不到
【发布时间】:2016-11-02 23:26:33
【问题描述】:
在定义 Mongoose 模式时,我花了半个小时试图找到关于 ref 属性的文档。没找到!
branch: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Branch' // <--- Where is the documentation for this property
}
谁能按我的英雄并链接它在哪里。
我尝试了以下方法:http://mongoosejs.com/index.html 并选择“阅读文档”,nada。 :(
【问题讨论】:
标签:
node.js
mongodb
mongoose
【解决方案1】:
This 是我在文档中唯一可以找到对ref 的引用的地方。简而言之,ref 用于让 mongoose 在您使用 .populate() 之类的东西时知道要填充什么模型。
例如,假设您的模型名称是Tree。如果你想找到所有的树并填充与每个 branch 关联的每个 tree 的所有信息,你可以使用以下代码:
Tree
.find({})
//note that this is lowercase branch
.populate('branch')
.exec((err,trees) => {
if(err) return console.log(err);
console.log(trees);
});