在流星中查找子阵列文档



我正在处理以下文档

{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [ 
{
 "uuid" : "123",
 "name" : "sugun",

"kiran" : [ 
{
 "uuid" : "456",
 "name" : "ghb"
}
       ]
}
]
}

我想从数组kiran的第一个文档中检索名称,并将其打印在表中。。。

这是我试过的

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

其中ProductManager是我的集合,在common.js 中定义

ProductManager = new Meteor.Collection("ProductManager"); 

这是我的模板

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

当我尝试这个时

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

我可以在mongo shell 中得到这个

{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [ 
{
 "uuid" : "123",
 "name" : "sugun",

"kiran" : [ 
{
 "uuid" : "456",
 "name" : "ghb"
}
       ]
}
]
}

但无法在表中打印Kiran数组的名称和uuid。。。。。。。plzz帮我解决这个问题。。。提前感谢

  1. 检查你的json格式,因为我觉得它无效
  2. 检查您的流星项目中是否缺少自动发布/不安全,如果是,则实现发布/子功能

Discovermeter对此有很好的指导Discovermeter

另一篇用于子文档搜索的stackoverflow帖子如何编写Mongo查询以查找条件为的子文档

最新更新