Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongooseJS-如何在Mongoose Populate Virtuals中指定文档数组中的对象字段?_Node.js_Mongodb_Mongoose - Fatal编程技术网

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
。它是否在数组中并不重要,只要使用
符号即可