MongooseJS - 如何在Mongoose Populate Virtuals中指定文档数组中对象的字段?

时间:2018-06-02 21:21:19

标签: node.js mongodb mongoose

假设我有以下文件

{
  name: "Johnny Doe",
  age: "55",
  organizations: [
  {name: "Org A",
   members: 22
  },
  {name: "Org B",
   members: 25
  }]
}

让我说我有另一个架构的虚拟设置

SomeSchema.virtual('organizations'){
 ref:"Person", //the collection to reference
 localField:"organization_name",  //the field in this Schema
 foreignField: [???]  //I want to access Person.organizations[].name
}

如何在localField / foreignField中指定我想要的字段?我在MongoDB中尝试了“组织。$。name”,但它不起作用,它返回一个空数组。

1 个答案:

答案 0 :(得分:0)

I found the answer. Just use field_array.field_of_array_object or in my example organizations.name. It doesn't matter if its in an array, just use the . notation.