【问题标题】: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);
   });

【讨论】:

    猜你喜欢
    • 2014-01-26
    • 2015-02-23
    • 1970-01-01
    • 2016-09-23
    • 2013-01-25
    • 2015-08-08
    • 2014-05-29
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多