如何检索流星中的子数组元素

How to retrieve the sub array elements in meteor

我正在处理以下文档

  {
    "_id" : 123344223,
    "firstName" : "gopal",
    "gopal" : [ 
    {
     "uuid" : "123",
     "name" : "sugun"
    }, 
    {
     "uuid" : "456",
     "name" :"kiran"
    }
             ]
}

我想从数组的第一个文档中检索名称并将其打印在 table...

这是我试过的

Template.table.helpers({
  ProductManager: function () {
    return ProductManager.find({_id:123344223},{gopal:{$elemMatch:{uuid:"123"}}});
  }
})    

ProductManager 是我的 collection 并在 common.js

中定义
ProductManager = new Meteor.Collection("ProductManager"); 

这是我的模板

<template name="table">
<table>
<thead>
<tr>
<th>NAME</th>
<th>UUID</th>
</tr>
</thead>
<tbody>
{{#each ProductManager}}
<tr>
<td>{{name}}</td>
<td>{{uuid}}</td>
</tr>
{{/each}}
</tbody>
</table>

当我尝试这个时

ProductManager.find({_id:123344223},{gopal:{$elemMatch:{uuid:"123"}}}); 

我可以在 mongo shell

中得到这个
{
 "_id" : 123344223,
"gopal" : [ 
{
 "uuid" : "123",
 "name" : "sugun"
}
}

但无法在 table 中打印名称和 uuid ...... 请帮我解决这个问题... 提前致谢

您基本上是在查询结果列表而不是数组中循环。要解决此问题,请查询特定文档:

使用 ProductManager.findOne 而不是 ProductManager.find(),因为您正在查找特定文档。

遍历 gopals 数组中的所有内容而不是游标本身:

{{#each ProductManager.gopal}}
....
{{/each}}

而不是循环 {{#each ProductManager}}...