Node.js MongooseJS-如何在Mongoose Populate Virtuals中指定文档数组中的对象字段?
假设我有以下文档Node.js MongooseJS-如何在Mongoose Populate Virtuals中指定文档数组中的对象字段?,node.js,mongodb,mongoose,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 loca
{
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中尝试过“organizations.$.name”,但它不起作用,它返回一个空数组。我找到了答案。只需使用
field\u array.field\u of_array\u object
或在我的示例中使用organizations.name
。它是否在数组中并不重要,只要使用
符号即可