【问题标题】:How to populate multiple foreign keys in mongoose如何在猫鼬中填充多个外键
【发布时间】:2014-03-31 20:06:18
【问题描述】:

假设我已经为 userId 中的 id 设置了对象引用:

{
    "__v" : 0,
    "_id" : ObjectId("530d8aa855df0c2d269a5a58"),
    "active" : false,
    "address" : "some ip address",
    "createDate" : ISODate("2014-02-26T06:33:12.367Z"),
    "displayName" : "coolguy55",
    "fullName" : "super cool guy 55",
    "userId" : [ 
        ObjectId("an ID"), 
        ObjectId("an ID")
    ]
}

我想知道是否可以在单个填充语句中填充这两个 id?

【问题讨论】:

    标签: node.js mongodb mongoose populate


    【解决方案1】:

    只要您的架构将 userId 定义为对应模型的 ObjectId 引用数组,调用 populate('userId') 将填充整个数组:

    MyModel.find().populate('userId').exec(...);
    

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2020-10-17
      • 2017-02-25
      • 2016-06-11
      • 2021-01-29
      • 2021-08-04
      • 2015-07-13
      • 2016-10-10
      相关资源
      最近更新 更多